javascript - ngModel 不更新深层次

标签 javascript angularjs

当我有一个以上级别的 ngModel 并且我在范围内以编程方式修改值时,该值不会在 UI 中更新。如果我在同一处特性上放置一个 $watch,它就会起作用。如何修复 ngModel

html:

<form ng-controller="testController">
    <input type="text" ng-model="coco.c.c.c">
    <input type="test" ng-model="super.s.s.s">
    <input type="test" ng-model="super2">
</form>

js:

var app = angular.module('app', []);

app.controller('testController', function ($scope) {
    $scope.super = {
        s: {
            s: {

            }
        }
    };

    $scope.coco = {
        c: {
            c: {

            }
        }
    };

    $scope.$watch('coco.c.c.c', function (value) {
        $scope.super = +value * 2;
        console.log(value);

    });
    $scope.$watch('super.s.s.s', function (value) {
        $scope.super2 = +value * 2;
    });
});
app.controller('testController2', function ($scope) {
    $scope.$watch('coco', function (value) {
        $scope.super = +value * 2;
        console.log(value);
    });
    $scope.$watch('super', function (value) {
        $scope.super2 = +value * 2;
    });
});

angular.bootstrap(文档, [app.name]);

http://jsfiddle.net/M5wzt/2/

最佳答案

我认为问题出在这一行

  $scope.super = +value * 2;

这里您正在更改 $scope.super 的内容。所以你不能再使用 $scope.super.s.s.s

不过,我不明白你想要完成什么。也许是这样的?

<form ng-controller="testController">
   <input type="text" ng-model="coco.c.c.c" />
   <input type="text" ng-model="super.s.s.s" />
   <input type="text" ng-model="super2" />
</form>

app.controller('testController', function ($scope) {
    $scope.super = {s:{ s:{ }}}

    $scope.coco = {
        c: {
            c: {

            }
        }
    };

    $scope.$watch('coco.c.c.c', function (value) {
        $scope.super.s.s.s = +value * 2;
        console.log("coco", value);

    });
    $scope.$watch('super.s.s.s', function (value) {
       $scope.super2 = +value * 2;
        console.log("super", value);
    });
});

关于javascript - ngModel 不更新深层次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23701600/

相关文章:

json - angularjs - ng-重复 : access key and value from JSON array object

javascript - 仅对可见控件应用 `$invalid`

angularjs - 如何在指令中插入 $compile 的 HTML 代码而不出现 $digest 递归错误?

javascript - 如何在打包的 Shiny 应用程序中引用 JS 文件中的图像?

javascript - 如何从 HTML5 视频播放器帧访问原始像素数据?

javascript - Socket.io 从接收者的浏览器发出,但不在发送者的浏览器上发出

javascript - jqGrid:以逗号作为小数点的数字格式

javascript - JavaScript 中的关键事件

javascript - 过滤器执行时如何向angularjs添加事件?

angularjs - MEAN.js : Get number of users stored in MongoDB