【问题标题】:AngularJS model object value can't change using dynamic keyAngularJS模型对象值不能使用动态键改变
【发布时间】:2017-08-16 02:10:48
【问题描述】:
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);

}]);

在这里我尝试更改 vm.active 对象属性,但它显示以下错误:

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

【问题讨论】:

  • 你考虑过使用路由器吗?

标签: javascript angularjs


【解决方案1】:

它显示错误是因为您的代码导致 $digest 无限循环,因为您试图在摘要循环期间更改模型。

要解决问题,请避免使用 $watch 更改模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多