Angularjs:2路绑定不包含模板工作 [英] Angularjs: 2 way binding not working in included template

查看:91
本文介绍了Angularjs:2路绑定不包含模板工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我失去了一些东西简单(重要)在这里。我使用包含的映射到某一值输入一个包含的模板:

I think I'm missing something simple (and important) here. I'm using an included template that contains an input that's mapped to some value:

<div ng-controller="Ctrl">
    <div ng-include src="'template.html'"></div>
</div>

<!-- template -->
<script type="text/ng-template" id="template.html">
    <input ng-model="testvalue" />
</script>

控制器:

function Ctrl($scope) {    
   $scope.testvalue= "initial value";
}​

警报的$ scope.testvalue值始终显示初始值,而不是更新后的值(当你在输入型)。帮助我欧比旺。你是我们唯一的希望。

Alerting the value of $scope.testvalue always shows the initial value, not the updated value (when you type in the input). Help me Obi-Wan. You're our only hope.

小提琴: http://jsfiddle.net/h5aac/

推荐答案

这是太常见结合到原始的,而不是一个对象。字符串的值被围而不传递给一个对象的引用。如果您使用的对象,而不是原始的,它工作正常。像这样的事情在你的范围。

This is the all too common of binding to a primitive instead of an object. The value of the string gets passed around and not a reference to an object. If you use an object instead of a primitive, it works fine. Something like this in your scope.

$scope.foo = {testvalue: "initial value"};

请参阅 http://jsfiddle.net/h5aac/2/

还有:

<一个href=\"http://stackoverflow.com/questions/13193929/using-ng-model-within-a-transcluded-directive-in-angularjs/13201939#13201939\">Using在AngularJS

<一个href=\"http://stackoverflow.com/questions/13148609/binding-issue-when-a-directive-in-a-ngrepeat/13150796\">binding问题,当在ngRepeat 指令

<一个href=\"http://stackoverflow.com/questions/12502237/angularjs-updating-scope-value-with-asynchronous-response/12502641\">AngularJS - 与异步响应更新范围值

我敢肯定有更多...

I'm sure there are more...

这篇关于Angularjs:2路绑定不包含模板工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆