简体   繁体   English

ngModel不要提升深层次

[英]ngModel don't upate deep level

When I have a ngModel with more that one level and I modify the value programmatically in the scope, the value is not updated in the UI. 当我有一个具有多个级别的ngModel并在范围内以编程方式修改值时,该值不会在UI中更新。 If I put a $watch on the same property, it works. 如果我将$ watch放在同一个属性上,它将起作用。 How can I fix ngModel 如何修复ngModel

html: 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: 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]); angular.bootstrap(document,[app.name]);

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

I think the problem is this line 我认为问题是这条线

  $scope.super = +value * 2;

here you are changing what $scope.super is. 在这里,您正在更改$ scope.super是什么。 So You can no more use $scope.super.sss 因此,您不能再使用$ scope.super.sss

I have not understood what is that you want to accomplish, though. 不过,我不知道您要完成什么。 Maybe something like this? 也许是这样的吗?

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

continuing the previous answer. 继续先前的答案。 On line 上线

$scope.super = +value * 2;

you are adding an object to a number creating a nan value, destroying your model reference hence no model since objects are attache and watched by reference this means your input has no model to watch to hence you are braking your chain 您将对象添加到创建nan值的数字中,破坏了模型引用,因此没有模型,因为对象已附加并通过引用监视,这意味着您的输入没有模型可监视,因此您正在制动链

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

and then you are trying to listen in a controller for changes in the other controllers scope?? 然后您尝试在一个控制器中侦听其他控制器范围内的更改? thats not going to work sorry you can only watch for thing happening in your scope 多数民众赞成在不起作用,对不起,你只能看着你的范围内发生的事情

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

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