Arif Arif -3 years ago 121
Javascript Question

AngularJS model object value can't change using dynamic key

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);

}]);


Here i try to change
vm.active
object property but it shows below error :


angular.js:14642 Error:
[$rootScope:infdig]http://errors.angularjs.org/1.6.5/$rootScope/..
at angular.js:88
at m.$digest (angular.js:18248)
at b.$apply (angular.js:18480)
at HTMLAnchorElement. (angular.js:27290)
at HTMLAnchorElement.dispatch (jquery-3.1.1.js:5201)
at HTMLAnchorElement.elemData.handle (jquery-3.1.1.js:5009)

Answer Source

It shows the error because your code is causing an infinite $digest loop because you are trying to change the model during the digest loop.

To solve the problem avoid changing the model using $watch.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download