角 - 内ngInclude调用时ngModel不更新ngInclude、ngModel

2023-09-13 02:53:05 作者:邋遢的很有范er

首先,该plunker: http://plnkr.co/edit/v1uTz5

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

这是我遇到的问题进行工作演示。

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

我有一个 NG-包括来包括部分。

里面的部分我有 ngModel 的文字输入和指令。

Inside the partial I have an text input with ngModel AND directive.

该模型会​​更新相应内部包括但外的任何交互包括被忽略。在 {{}测试} 外包括不更新,但 {{}测试} 里面呢。

The model updates accordingly inside the include, but any interaction outside the include is ignored. The {{test}} outside the include doesn't update, but the {{test}} inside does.

该指令被调用时,处理输入键并调用正确的范围和功能。但是, $ scope.test 变量从未更新过,但 $ scope.testFinal 更新和 NG-包括模板正确地呈现它。尝试重置 $ scope.test 模式也不行。

The directive, when called, handles the enter key and calls the correct scope and function. However, the $scope.test variable has never been updated, but $scope.testFinal is updated and the ng-include template renders it appropriately. Trying to reset the $scope.test model does not work either.

我失去了一些东西在这里?或者这是与指令或用错误的 NG-包括

Am I missing something here? Or is this a bug with the directive or with the ng-include?

推荐答案

而不是使用primitiive来定义变量,让它的对象。

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

简单的说明性的例子:

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

现在做相同的对象:

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"*/

你Plunker修改

阅读这篇文章的角度维基