0
我有以下提供程序導致應用程序無法執行任何操作,但在控制檯上沒有錯誤...我已經查看了幾個小時無濟於事。角度縮小 - 導致應用程序停止
(function (angular) {
"use strict";
angular.module('app').provider('templateRepository', function() {
var templateKey = 'template:',
useCache = true,
cacheDuration = 30,
autoCleanCache = true,
cleanCacheInterval = 30;
return {
$get: ['$http', '$q', '$interval', function ($http, $q, $interval) {
var getFromCache = function (key) {
return JSON.parse(localStorage.getItem(key));
},
getFromServer = function (name) {
return $http.get('Template/Index?name=' + name).then(function (response) {
return response.data;
});
},
isExpired = function (item) {
return ((parseInt(new Date() - new Date(item.timestamp))/(1000 * 60)) >= cacheDuration);
},
removeFromCache = function (key) {
localStorage.removeItem(key);
},
saveToCache = function (key, value) {
localStorage.setItem(key, JSON.stringify({
timestamp: new Date().toUTCString(),
value: value
}));
},
cleanCache = function() {
for (var key in localStorage) {
if (key.substring(0, 9) === templateKey) {
if (isExpired(JSON.parse(localStorage.getItem(key)))) {
removeFromCache(key);
}
}
}
};
if (useCache && autoCleanCache) {
$interval(function() {
cleanCache();
}, (cleanCacheInterval * 60 * 1000));
}
return {
get: function (name) {
var key = templateKey + name,
d = $q.defer(),
item;
if (useCache) {
item = getFromCache(key);
if (item === null) {
getFromServer(name).then(function (template) {
saveToCache(key, template);
d.resolve(template);
});
} else {
if (isExpired(item)) {
getFromServer(name).then(function (template) {
saveToCache(key, template);
d.resolve(template);
});
} else {
d.resolve(item.value);
}
}
} else {
getFromServer(name).then(function (template) {
d.resolve(template);
});
}
return d.promise;
}
};
}],
configureCache: function (enable, duration, autoClean, cleanInterval) {
useCache = enable;
cacheDuration = duration;
autoCleanCache = autoClean;
cleanCacheInterval = cleanInterval;
}
};
});
}(this.angular));
這可能沒有關係,但爲什麼'this.angular''而不是'angular'作爲變量被傳入閉包? – dcodesmith
@dcodesmith沒有區別。 – Sam