[英]How to pass string argument to template using Angular
For the following template: 对于以下模板:
<h2 class="viewTitle">{{viewTitle | translate}}</h2>
I'm passing sometimes a translatable string, sometimes a variable in scope. 我有时会传递可翻译的字符串,有时会传递范围内的变量。 If I include the template like this:
如果我包括这样的模板:
<div ng-init="viewTitle = 'translatable.title'" ng-include="'views/templates/view-title.html'"/>
Things work. 事情正常。 However, I cannot pass a variable from my scope.
但是,我无法从范围中传递变量。 How to accomplish both?
如何实现两者?
your main issue is this: 您的主要问题是:
How to set scope property with ng-init? 如何使用ng-init设置范围属性? .
。 you are reading a scope attribute with ng-init before angular had time to write it.
您需要先用ng-init读取scope属性,然后再用angular编写它。 You cannot access $scope attributes in ng-init.
您不能在ng-init中访问$ scope属性。 instead set the $scope attributes in your controller (js code).
而是在控制器中设置$ scope属性(js代码)。
these are additional issues you will probably encounter: 这些是您可能会遇到的其他问题:
this will work (see below), but you are probably encountering the typical angular scoping issues, for which there are two possible solutions: 这将起作用(请参阅下文),但是您可能会遇到典型的角度范围界定问题,有两种可能的解决方案:
Always follow the dot-rule ( https://egghead.io/lessons/angularjs-the-dot ), will help you to avoid many scoping issues in our career 始终遵循点规则( https://egghead.io/lessons/angularjs-the-dot ),将帮助您避免我们职业生涯中的许多范围界定问题
or use the "controller as" syntax ( http://toddmotto.com/digging-into-angulars-controller-as-syntax/ ). 或使用“控制器为”语法( http://toddmotto.com/digging-into-angulars-controller-as-syntax/ )。
template: 模板:
<h2 class="viewTitle">{{myScopeAttributeInParentScope | translate}}</h2> data: {{myScopeAttributeInParentScope}}
using with String: 与String一起使用:
<div ng-init="myScopeAttributeInParentScope=translatable.title'}" ng-include="'views/templates/view-title.html'"/>
using with scope attribute: 与范围属性一起使用:
<div ng-include="'views/templates/view-title.html'"/>
parent Scope: {{myScopeAttributeInParentScope}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.