angularjs 常用方法

一 angular的copy和extend

1.angular.extend() 
angular.extend():依次将第二个参数及后续的参数的第一层属性(不管是简单的属性还是对象)拷贝,赋给第一个参数的第一层属性。也就是说,如果是对象,那么引用的是同一个对象,并返回第一个参数对象。 
用法1:angular.extend(b,a); 将a拷贝给b 
将a拷贝给b 
看一个具体的例子:

var a = {
  name : 'bijian',
  address : 'shenzhen',
  family : {
    num : 6,
    amount : '80W'
  }
};
var b = {};
var r = angular.extend(b, a);
console.log('a:' + JSON.stringify(a));
console.log('b:' + JSON.stringify(b));
console.log('r:' + JSON.stringify(r)); 

b.address = 'hanzhou';
b.family.amount = '180W';
console.log('a:' + JSON.stringify(a));
console.log('b:' + JSON.stringify(b));
console.log('r:' + JSON.stringify(r));

结果:

a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
b:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
r:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"180W"}}
b:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}}
r:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}}

用法2:angular.extend(b, a, z); 相继将对象a,z拷贝赋给b 
举个例子:

var a = {
  name : 'bijian',
  address : 'shenzhen',
  family : {
    num : 6,
    amount : '80W'
  }
};
var z = {
  family : {
    amount : '150W',
    mainSource : '经营公司'
  }
};
var b = {};
var r = angular.extend(b, a, z);
console.log('a:' + JSON.stringify(a));
console.log('b:' + JSON.stringify(b));
console.log('r:' + JSON.stringify(r)); 

b.address = 'hanzhou';
b.family.amount = '180W';
console.log('a:' + JSON.stringify(a));
console.log('b:' + JSON.stringify(b));
console.log('r:' + JSON.stringify(r));

结果:

a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
b:{"name":"bijian","address":"shenzhen","family":{"amount":"150W","mainSource":"经营公司"}}
r:{"name":"bijian","address":"shenzhen","family":{"amount":"150W","mainSource":"经营公司"}}
a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
b:{"name":"bijian","address":"hanzhou","family":{"amount":"180W","mainSource":"经营公司"}}
r:{"name":"bijian","address":"hanzhou","family":{"amount":"180W","mainSource":"经营公司"}}

2.angular.copy() 
angular.extend()是浅拷贝,而angular.copy()是深拷贝。这两个有什么区别呢,看同样的例子。 
用法1:angular.copy(a,b); 将对象a中的属性深度拷贝给b 
这里a和b不会相互依赖,a和b是完全脱离关联的

var a = {  
    name : 'bijian',  
    address : 'shenzhen',  
    family : {  
        num : 6,  
        amount : '80W'  
    }  
};  
var b = {};  
var r = angular.copy(a, b);  
console.log('a:' + JSON.stringify(a));  
console.log('b:' + JSON.stringify(b));  
console.log('r:' + JSON.stringify(r));  
  
b.address = 'hanzhou';  
b.family.amount = '180W';  
console.log('a:' + JSON.stringify(a));  
console.log('b:' + JSON.stringify(b));  
console.log('r:' + JSON.stringify(r));  

结果:

a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  
b:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  
r:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  
a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  
b:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}}  
r:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}} 

用法2:var r=angular.copy(a); 将对象a对象的属性深度拷贝给对象r

var a = {  
    name : 'bijian',  
    address : 'shenzhen',  
    family : {  
        num : 6,  
        amount : '80W'  
    }  
};  
var r = angular.copy(a);  
console.log('a:' + JSON.stringify(a));  
console.log('r:' + JSON.stringify(r));  
  
a.address = 'hanzhou';  
a.family.amount = '180W';  
console.log('a:' + JSON.stringify(a));  
console.log('r:' + JSON.stringify(r)); 

结果:

a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  
r:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  
a:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}}  
r:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}  

 

二 angular判断数据类型

方法说明
angular.isString() 判断是否是字符串
angular.isObject() 判断是否是对象,注意,它不把null视为对象,数组是对象
angular.isArray() 判断是否为数组
angular.isDtae 判断是否是时间
angular.isNumber 判断是否是数字
angular.isDefined() 判断是否存在
angular.isUndefined() 判断是否是undefined
angular.isFunction() 判断是否为函数
angular.isElement() 判断是否是一个DOM元素或者包装的jQuery元素
var div = angular.element("#findMe");
var str = "1";
var num = 1;
var obj = { value: 1 };
var arr = [obj, str];
var date = new Date();
var fn = function(){
     // your code
};
var divIsElement = angular.isElement(div);  //divIsElement = true
var strIsString = angular.isString(str);  //strIsString = true
var numIsNumber = angular.isNumber(num);  //numIsNumber = true
var objIsObject = angular.isObject(obj);  //objIsObject = true
var arrIsArray = angular.isArray(arr);  //arrIsArray = true
var dateIsDate = angular.isDate(date);  //dataIsDate = true
var fnIsFunction = angular.isFunction(fn);  //fnIsFunction = true
var nullIsDefined = angular.isDefined(null);  //nullIsDefined = true
var undefinedIsUndefined = angular.isUndefined(undefined);  //undefinedIsUndefined = true 

 

三 angular.forEach()

var array = ['a','b','c'];
angular.forEach(array,function(value,i){
 console.log(value,i);
 console.log(this);
});

遍历数组,对象也可以。value表示值,i表示索引,而this表示window对象。

 

四 angular的json和字符串转换

angular为我们提供了原生的JSON.parse()和JSON.stringigy()方法。 
angular.fromJson():字符串转json对象 
angular.toJson():json对象转字符串

var json = '{"name":"liSi", "password":"321"}';
var obj = angular.fromJson(json);


var obj ={
  name:"liSi", password:"321"
}
var str = angular.toJson(obj, true);

 

五 大小写转换

angular.uppercase('hello');
angular.lowercase('HELLO');

 

posted @ 2018-04-23 10:39  LittleMoon  阅读(1402)  评论(0编辑  收藏  举报