Javascript AngularJS模型对象值可以';不能使用动态键进行更改
在这里,我尝试更改Javascript AngularJS模型对象值可以';不能使用动态键进行更改,javascript,angularjs,Javascript,Angularjs,在这里,我尝试更改vm.active对象属性,但它显示以下错误: angular.js:14642错误: [$rootScope:infdig]$rootScope/。。 在角上。js:88 m.$digest(angular.js:18248) 在b.$apply(angular.js:18480) 在兰开夏。(angular.js:27290) 在htmlanchorement.dispatch(jquery-3.1.1.js:5201) 位于htmlanchorement.elemData
vm.active
对象属性,但它显示以下错误:
angular.js:14642错误:
[$rootScope:infdig]$rootScope/。。
在角上。js:88
m.$digest(angular.js:18248)
在b.$apply(angular.js:18480)
在兰开夏。(angular.js:27290)
在htmlanchorement.dispatch(jquery-3.1.1.js:5201)
位于htmlanchorement.elemData.handle(jquery-3.1.1.js:5009)
它显示错误,因为您的代码导致了一个无限的$digest循环,因为您试图在digest循环期间更改模型
要解决此问题,请避免使用$watch更改型号。您考虑过使用路由器吗?
app.controller('indexController', ['$scope', '$location', 'authService', function ($scope, $location, authService) {
var vm = this;
vm.$onInit = function () {
vm.active = {
"home": true,
"welcome": false,
"user": false,
"logout": false,
"login": false,
"signup":false
};
};
$scope.$watch('vm.active', function (newObj, oldObj) {
Object.keys(newObj).filter(function (key) {
vm.active[key] = newObj[key] !== oldObj[key];
return vm.active[key];
});
}, true);
}]);