繁体   English   中英

ngModel不要提升深层次

[英]ngModel don't upate deep level

当我有一个具有多个级别的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(document,[app.name]);

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

我认为问题是这条线

  $scope.super = +value * 2;

在这里,您正在更改$ scope.super是什么。 因此,您不能再使用$ scope.super.sss

不过,我不知道您要完成什么。 也许是这样的吗?

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

继续先前的答案。 上线

$scope.super = +value * 2;

您将对象添加到创建nan值的数字中,破坏了模型引用,因此没有模型,因为对象已附加并通过引用监视,这意味着您的输入没有模型可监视,因此您正在制动链

http://jsfiddle.net/d65yan/M5wzt/4/

然后您尝试在一个控制器中侦听其他控制器范围内的更改? 多数民众赞成在不起作用,对不起,你只能看着你的范围内发生的事情

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM