AngularJs 常用函数

/**
* [intersect 取两个数组的交集]

var firstArray = [1,3,5];
var secondArray = [2,5,8];

var result
*/
.filter('intersect', function () {
return function (arr1, arr2) {
var temp = [];
for (var i = 0; i < arr1.length; i++) {
for (var k = 0; k < arr2.length; k++) {
if (arr1[i] == arr2[k]) {
temp.push(arr1[i]);
break;
}
}
}
return temp;
}
})

/**
* [guidGenerator 生成GUID]
*/
.factory("guidGenerator", function () {
var generatePart = function () {
var guidPartNumber = (Math.random() * 0x10000) | 0;
return (guidPartNumber + 0x10000).toString(16).substring(1).toUpperCase();
};

return function () {
return generatePart()
+ generatePart()
+ "-"
+ generatePart()
+ "-"
+ generatePart()
+ "-"
+ generatePart()
+ "-"
+ generatePart()
+ generatePart()
+ generatePart();
}
})

/**
* [CryptoJS 加密解密]
*/
.factory('crypto',[function(){
var aesKey; //for AES encryption/decryption

// encrypt data though AES, with aesKey
function encrypt(data) {
if (aesKey == null) {
throw "AES key not initialized"
}
data = CryptoJS.AES.encrypt(data, aesKey).toString();
return data;
}

// decrypt data though AES, with aesKey
function decrypt(hash) {
if (aesKey == null) {
throw "AES key not initialized"
}
hash = CryptoJS.AES.decrypt(hash, aesKey).toString(CryptoJS.enc.Utf8);
return hash;
}
}])

/**
* [LocalStorage 本地存储服务]
*/
// the storage persisting layer
.factory('storage', [function () {
return {
save: function (key, value) {
window.localStorage.setItem(key, value);
},

load: function (key) {
return window.localStorage.getItem(key);
}
}

}])

 

转载于:https://www.cnblogs.com/code-charmer/p/4717988.html

你可能感兴趣的:(AngularJs 常用函数)