Angular - ngModel not updating when called inside ngInclude

前端 未结 3 635
傲寒
傲寒 2021-01-03 23:10

First and foremost, the plunker: http://plnkr.co/edit/v1uTz5

This is a working demo of the issue I am running into.

I have a ng-include to inc

3条回答
  •  青春惊慌失措
    2021-01-04 00:10

    Instead of using a primitiive to define the variable, make it an object.

    $scope.model={test:''};
    

    Directives create their own scope for each item. When you equate a primitive to a new scope variable, it has no binding to the original, however when original is an object, a reference is created , not a copy, and changes made in one will reflect in the other

    Simple explanatory example:

    var a ='foo';
    var b= a;
    /* now change a*/
    a='bar';
    alert( b) // is still 'foo'
    

    now do the same with object:

    var obj_1= {a:'foo'};
    var obj_2=obj_1;
    /* now change obj_1.a*/
    obj_1.a='bar';
    alert( obj_2.a) // change to obj_1 will also change obj_2 and alert returns "bar"*/
    

    Your Plunker Modified

    Read this article on angular wiki for more detailed explanation

提交回复
热议问题