Angular封装了一系列公共方法,帮助我们更简单的使用 JS。
1、angular.bind()
angular.bind 类似于 Function.prototype.bind,实现函数柯里化,返回一个函数代理。eg:
//函数原型
angular.bind(/*this对象*/self, /*要封装的function*/fn, /*参数列表*/args);
//原始函数
function fun(arg1, arg2, arg3) {
console.log(this);
console.log(arg1);
console.log(arg2);
console.log(arg3);
}
fun('arg1', 'arg2', 'arg3');
//如果几个常用参数都是固定的,而且该函数被调用频繁,那么就可以包装一下
var fun2 = angular.bind(window, fun, 'arg1', 'arg2');
//新的调用方式
fun2('arg3');
2、angular.bootstrap()
用于使用 angular 执行渲染元素。也是 angular 的启动方法,如果没有 在页面上指定ng-app,必须要手动调用该函数进行启动。
angular.bootstrap(/*Dom元素*/element, /*Array/String/Function*/[modules], /*Object*/[config]);
//常规用法
angular.bootstrap(document, ['app'])
//带配置项
angular.bootstrap(document, ['app'], {strictDi: true/*Defaults: false*/})
3、angular.copy()
Angular.copy 用于复制对象,由于 angular 的双向绑定特点,那么如果直接操作 $scope 对象,那么很容易就会改变 ui 的显示,这个时候就需要借助 angular.copy 来创建一个对象副本,并进行操作。
//原型
angular.copy(source, [destination]);
var obj = {a: 1};
var obj2 = angular.copy(obj);
var obj3;
angular.copy(obj, obj3);
console.log(obj2 === obj) //false
console.log(obj3 === obj) //false
var obj4;
//第二个和参数和返回值是相等的,而且第二个参数不管以前是什么,都会被重新赋值
var obj5 = angular.copy(obj, obj4);
console.log(obj4 === obj5); //true
4、angular.element()
等价与 jQuery 的选择器,如果在 angular 之前没有引入 jQuery,那么就使用 jqLite 包装。
angular.element('body');
//等价于
$('body');
//用法
var $body = angular.element('body');
5、angular.equals()
用于比较两个对象是否相等,如下示例的规则和 JS 有区别,注意识别。
var obj1 = {a: 1};
v