如果有人拥有带有键 -> 值对的数据存储类型的服务,则几乎没有性能提示:
如果您有一个名为dataStore的服务,您可以在大数据对象更改时更新时间戳。这样,您无需深入观察整个对象,而只是观察时间戳以进行更改。
app.factory('dataStore', function () {
var store = { data: [], change: [] };
// when storing the data, updating the timestamp
store.setData = function(key, data){
store.data[key] = data;
store.setChange(key);
}
// get the change to watch
store.getChange = function(key){
return store.change[key];
}
// set the change
store.setChange = function(key){
store.change[key] = new Date().getTime();
}
});
而在指令中,您只是在观察要更改的时间戳
app.directive("myDir", function ($scope, dataStore) {
$scope.dataStore = dataStore;
$scope.$watch('dataStore.getChange("myKey")', function(newVal, oldVal){
if(newVal !== oldVal && newVal){
// Data changed
}
});
});