Javascript Angularjs ControllerAs更换$watchCollection
我一直在重构我的angularjs应用程序以使用controllerAs,我想看看是否可以去掉所有$scope.$watch函数。我通过使用getter和setter定义属性来管理它,这非常有效。我陷入困境的是$watchCollection的替代品。例如,我有一个指令,其属性定义为“params”。这是一个具有子属性的对象。在我可以使用$scope.$watch('params',function(){…},true)或使用$watchCollection之前。现在不会调用setter,因为更改子参数时不会设置实际对象。下面是我提出的解决方案,但我希望有更好的解决方案。此外,我知道如果需要,我仍然可以使用$watch和$watchCollection。只是看看是否有办法避免它。params属性是动态的,因此不会预先知道子属性Javascript Angularjs ControllerAs更换$watchCollection,javascript,angularjs,Javascript,Angularjs,我一直在重构我的angularjs应用程序以使用controllerAs,我想看看是否可以去掉所有$scope.$watch函数。我通过使用getter和setter定义属性来管理它,这非常有效。我陷入困境的是$watchCollection的替代品。例如,我有一个指令,其属性定义为“params”。这是一个具有子属性的对象。在我可以使用$scope.$watch('params',function(){…},true)或使用$watchCollection之前。现在不会调用setter,因为更
var dynamicVariables = {};
function defineChildProperty(obj,i){
if(!dynamicVariables.hasOwnProperty(i)){
dynamicVariables[i] = obj[i];
}
Object.defineProperty(obj,i,{
get:function(){return dynamicVariables[i];},
set:function(val){
if(obj[i] === val){
return;
}
dynamicVariables[i] = val;
fetchChart();
}
});
}
function getChildProperties(obj){
for(var i in obj){
if(obj.hasOwnProperty(i)){
defineChildProperty(obj,i);
}
}
}