AngularJS方法 —— angular.bind

描述:

  上下文,函数以及参数动态绑定,返回值为绑定之后的函数. 其中args是可选的动态参数,self在fn中使用this调用。

使用方法:

  angular.bind(self,fn,args );

参数:

参数名称参数类型描述
selfObjectfn的上下文对象,使用this调用
fnfunction被绑定的function
args*传入fn中的参数(可选的)

返回值:返回动态绑定之后的函数;

实例:

<!DOCTYPE html>

  <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>bind例子</title>

    <head>

      <script src="../angular-1.3.0.14/angular.js"></script> <script type="text/javascript">

        var self = { name: 'boyi' };//示例1--带参数

        var f = angular.bind(self, //绑定对象,作为函数的上下文 //被绑定的函数 function (age) { alert(this.name + ' is ' + age + ' !'); },//绑定的参数,可省略 '15' );

        f();//调用绑定之后的function

        //示例2--不带参数

        var m = angular.bind(self, //绑定对象,作为函数的上下文 //被绑定的函数 function (age) { alert(this.name + ' is ' + age + ' !'); }//省略参数 );

        m(3);//调用传参的函数

      </script>

    </head>

    <body>

    </body>

</html>