首页 > 代码库 > angular中的ng.function

angular中的ng.function

网址:http://www.angularjsapi.cn/#/copy

angular.bind

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

使用方法是:angular.bind(self,fn,args)

其中self的参数类型是obj,fn的参数类型是function,args传入fn的参数。

例子: 

var self = {zhaunglongfei:"boyi"};
var f = angular.bind(self,function (age) {
console.log(this.zhaunglongfei + age)
},"19");
f();
var f = angular.bind(self,function (age) {
console.log(this.zhaunglongfei + age)
});
f("20");

angular.bootstrap()

描述:此方法用于手动加载angularjs动态模板。(官方翻译:注意基于端到端的测试不能使用此功能来引导手动加载,他们必须使用ngapp。 angularjs会检测这个模板是否被浏览器加载或者加载多次并且在控制台给出警告在加载其他模块的时候,这防止了奇怪的结果,在实际应用中,angularjs在尝试其它的多个实例来研究DOM)。

使用方法:

angular.bootstrap(element,[modules],[config]);

例子:

<body>
        <div ng-controller="WelcomeController">
        <span ng-bind="greeting"></span>
        </div>
</body>
<script type="text/javascript">
        var app = angular.module(‘demo‘, [])
        .controller(‘WelcomeController‘, function ($scope) {
        $scope.greeting = ‘Welcome!‘;
        });
        angular.bootstrap(document, [‘demo‘]);
</script>

angular.copy();
描述:
复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象)。
如果省略了destination,一个新的对象或数组将会被创建出来;
如果提供了destination,则source对象中的所有元素和属性都会被复制到destination中;
如果source不是对象或数组(例如是null或undefined), 则返回source;
如果source和destination类型不一致,则会抛出异常。
注意:这个是单纯复制覆盖,不是类似继承

例子:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src=http://www.mamicode.com/"../angular-1.3.0.14/angular.js"></script>
        <script type="text/javascript">
        angular.module(‘copyExample‘, [])
        .controller(‘ExampleController‘, [‘$scope‘, function ($scope) {
        $scope.master = {};
        $scope.update = function (user) {
        // Example with 1 argument
        $scope.master = angular.copy(user);
        };
        $scope.reset = function () {
        // Example with 2 arguments
        angular.copy($scope.master, $scope.user);
        };
        $scope.reset();
        }]);
        </script>
</head>
<body ng-app="copyExample">
        <div ng-controller="ExampleController">
        <form novalidate class="simple-form">
            Name: <input type="text" ng-model="user.name" /><br />
            E-mail: <input type="email" ng-model="user.email" />(输入email格式)<br />
            Gender: <input type="radio" ng-model="user.gender" value=http://www.mamicode.com/"male" />male
        <input type="radio" ng-model="user.gender" value=http://www.mamicode.com/"female" />female<br />
        <button ng-click="reset()">RESET</button>
        <button ng-click="update(user)">SAVE</button>
        </form>
<pre>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
        </div>
</body>
</html>
 

angular中的ng.function