admin管理员组文章数量:1327660
I have an issue when using ng-model
inside ng-transclude
.
As ng-transclude
creates child scopes the value can't be set to the outer scope anymore.
Without ng-transclude everything works fine:
{{text}}
<div>
<textarea ng-model="text"></textarea>
</div>
With ng-transclude the text won't update as the textarea modifies only the child scope:
{{text}}
<pane>
<textarea ng-model="text"></textarea>
</pane>
Is there any other way then using ng-model="$parent.text"
?
I have an issue when using ng-model
inside ng-transclude
.
As ng-transclude
creates child scopes the value can't be set to the outer scope anymore.
Without ng-transclude everything works fine:
{{text}}
<div>
<textarea ng-model="text"></textarea>
</div>
With ng-transclude the text won't update as the textarea modifies only the child scope:
{{text}}
<pane>
<textarea ng-model="text"></textarea>
</pane>
http://plnkr.co/edit/GKf7WhnnItVNeBpvSB0F?p=preview
Is there any other way then using ng-model="$parent.text"
?
-
How about putting a
$watch
on text in the scope and fire an$emit
event? – Raghavendra Commented Jul 11, 2014 at 17:22
1 Answer
Reset to default 10as the $parent may refer to a different scope, depending on the context, it is advisable that you declare an object to hold properties you intend to write into (e.g. $scope.data = {text: "foo"};
) , so that when the ng-model is trying to write the value (via ng-model="data.text"
), it will have to make a "read" first, looking along the prototype chain, until it finally reaches the "data" property on the desired scope (assuming there is no other scope that has that property along the way).
This approach follows the "always use the dot in ng-model" rule.
(side note: another possible approach is to use an alias for the controller, assuming it is available in the angular version you are using).
<div ng-controller="ExampleController">
{{my.text}}
<pane>
<textarea ng-model="my.text"></textarea>
</pane>
</div>
http://plnkr.co/edit/aESrHtuSH9cd9ljyQAfH?p=preview
本文标签: javascriptngmodel inside ngtranscludeStack Overflow
版权声明:本文标题:javascript - ng-model inside ng-transclude - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228784a2436827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论