Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS模型对象值可以';不能使用动态键进行更改_Javascript_Angularjs - Fatal编程技术网

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

}]);