我的 angularjs Controller 上有一个属性,在我的应用程序中该属性被修改为具有不同的属性,但该属性是动态创建的。我需要在每个动态创建的属性上添加 $watch
,但我不知道该怎么做。
该属性是在 Controller 上创建的,如下所示:
function FilterContentController($rootScope, $scope, filterOptions, $state, $stateParams, dataService) {
var self = this;
this.$scope = $scope;
this.currentMobileFilters = {};
}
然后我有一个方法pushFilter
:
function (category, value) {
var self = this;
if (!self.currentMobileFilters[category]) {
self.currentMobileFilters[category] = [];
}
if (self.isSelectedMobileFilterValue(category, value)) {
// Remove
self.currentMobileFilters[category] = _.without(self.currentMobileFilters[category], value);
} else {
// Add
self.currentMobileFilters[category].push(value);
}
};
如果 currentMobileFilters
对象不存在且参数名称为 category
,则 pushFilter
会在 currentMobileFilters
对象上添加一个新属性,然后推送过来参数值
的数组。
我需要做的是跟踪在 currentMobileFilters
上创建的每个属性的数组更改。
这可能吗?
最佳答案
Angular $watch 方法接受相等监视类型的第三个参数。请参阅文档 here在 $watch 部分下。
关于javascript - $监视动态创建的对象新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006052/