簡體   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