Angular API 核心组件之 Function
1 angular.lowercase 将给定得字符串转换成小写的形式
angular.lowercase(string)
2 angular.uppercase 将给定得字符串转换成大写形式
angular.uppercase(string)
3 angular.forEach(obj, iterator ,[context]) :
为对象或者数组中的每一项执行迭代器 function函数,function函数有两个参数value,key。value对象中每一个属性的值,或者是数组中的元素。而key则是属性名或者是数组的下标index。而可选参数context是function执行环境。
var values={name:'misko','gender':'male'}; var log=[]; angular.forEach(values,function(value,key){ this.push(key + ':' + value) },log) // log 是function执行的上下文 指定了this的值。
4 angular.extend (dst , src)
将源对象src中的所有属性全部拷贝到目标对象 ,可以指定多个src对象
<!DOCTYPE html> <html ng-app="copyExample"> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/angular.js"></script> </head> <body ng-controller="myCopy"> <div>{{dest}}</div> <script> angular.module('copyExample', []) .controller('myCopy', ['$scope', function($scope) { var source = {"name":"aviva","age":"26","gender":"female"}; var source2 = {"job":"coder","address":"bj"}; $scope.dest = {"name":"David","age":"28","gender":"male"}; angular.extend($scope.dest,source,source2); console.log($scope.dest); }]); </script> </body> </html>
5 angular .noop
这个函数不执行任何操作。例子:
function foo(callback){ var result = calculateResult(); (callback || angular.noop)(result); }
6 angular .identity
这个函数返回他的第一个参数,例子:
function transformer(transformationFn,value) { return (transformationFn || angular.indentity)(value) }
7 angular.isUndefined
确定参数是否是未定义undefined
angular.isUndefined(value);
8 angular.isDefined
确定给定的参数是否是已经定义了
angular.isDefined(value)
9 angular.isObject
确定一个给定的参数是否是对象,与Javascript中的typeof不同,null不被认为是object对象,注意:js中数组也是对象。
angular.isObject(value)
10 angular.isString
确定给定的参数是否是字符串
angular.isString(value)
11 angular.isNumber
确定给定的参数是否是数字
angular.isNumber(value)
12 angular.isDate
确定给定的参数是否是日期
angular.isDate(value)
13 angular.isArray
确定给定的参数是否是数组
angular.isArray(value)
14 angular.isFunction
确定给定的参数是否是函数
angular .isFunction(value)
15 angular.isElement
确定给定的参数是一个 DOM元素(或者是包装过的 jQuery元素)
angular.isElement(value)
16 angular.copy(source,[destination])
深层复制source ,source必须是对象Object 或者数组 array
- 如果destination(目标)没有指定,会创建一个对象或者数组
<script type="text/javascript"> angular.module('myCopy',[]) .controller('copyCtrl',function(){ var source = [1,2,3,4]; var destination = angular.copy(source); console.log(destination); }) </script>
- 如果destination(目标)指定了,它所有的属性(对象)或者元素(数组)都会被删除,然后把source中的属性或者元素拷贝进去。
angular.module('myCopy',[]) .controller('copyCtrl',function(){ var source = [1,2,3,4]; var destination = [4,5,6,7]; angular.copy(source,destination); console.log(destination);// [1,2,3,4] })
- 如果source不是对象或者数组,将会返回source本身
angular.module('myCopy',[]) .controller('copyCtrl',function(){ var source = "this is a string"; var destination = [4,5,6,7]; angular.copy(source,destination); console.log(destination); //["t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "s", "t", "r", "i", "n", "g"] })
- 如果source和destination完全相同,会抛出一个异常。
17 angular.equals
确定给定的两个对象或者两个参数是否相同,参数可以是对象,数组,正则表达式。
传递的两个参数只要符合以下中的一项,就会被认为是相同的
- 通过 “===”比较两个对象或者参数
- 传递的对象或者值是相通的类型,并且他们所有的属性的值通过angualar.equals 比较都相等
- 传递的两个参数都是NAN(在js中 NAN与NAN比较会返回false,但是在这里我们认为是相等的)
- 传递的两个参数都代表相同的正则表达式(在js中 /abc/==/abc/返回false,但是我们认为两个正 则表达式文本内容匹配时,就是相等的)
18 angular.bind(self,fn,args)
返回一个函数,它的执行环境时self(fn中的this=self),args是可选参数
angular.module('bindExample',[]) .controller('bindCtrl',function(){ var f = angular.bind({a: 'xx'}, function(){ console.log(this.a); } ); f();//'xx' })
19 angular.toJson
将input序列化成json格式的字符串,带有$前缀的属性将会被剥夺,因为angular在内部使用这个符号
angular.toJson(obj,[pretty]) pretty=true/false r如果pretty为true,那么输出的json格式会包含换行和空格
20 angular.fromJson(json)
将json格式反序列化
21 angular.bootstrap
利用这个函数可以手动的触发angular 应用程序
<script type="text/javascript"> angular.module('app',[]).controller('bootstrapCtrl',function(){ console.log('123'); }); angular.bootstrap(document.documentElement,['app']); </script>
22 angular.element
将 DOM元素或者 html 字符串包装成 jQuery 元素
如果引用了jQuery,angular.element 就是jQuery函数得一个别名,如果没有引用jQuery,angular.element则代表了jQuery的一个子集,叫做 “jQuery Lite” 或者 “jqLite”;
<script type="text/javascript"> angular.module('app',[]) .controller('eleCtrl',function(){ //console.log(12); var divs=angular.element(document.getElementsByTagName('div')).eq(0).addClass('red'); console.log(divs); }); angular.bootstrap(document.documentElement,['app']); </script>
jqLite 只提供以下的方法:
addClass()
after()
append()
attr()
bind()
- 不支持命名空间,选择器,event 数据 Does not support namespaces, selectors or eventDatachildren()
- 不支持选择器 Does not support selectorsclone()
contents()
css()
data()
empty()
eq()
find()
-只支持通过标签名查找 Limited to lookups by tag namehasClass()
html()
next()
- 不支持选择器 Does not support selectorson()
-不支持命名空间,选择器,event 数据 Does not support namespaces, selectors or eventDataoff()
- 不支持命名空间,选择器Does not support namespaces or selectorsone()
- 不支持命名空间,选择器Does not support namespaces or selectorsparent()
-不支持选择器 Does not support selectorsprepend()
prop()
ready()
remove()
removeAttr()
removeClass()
removeData()
replaceWith()
text()
toggleClass()
triggerHandler()
- Passes a dummy event object to handlers.unbind()
- Does not support namespacesval()
wrap()