繁体   English   中英

如何使用Angular将字符串参数传递给模板

[英]How to pass string argument to template using Angular

对于以下模板:

<h2 class="viewTitle">{{viewTitle | translate}}</h2>

我有时会传递可翻译的字符串,有时会传递范围内的变量。 如果我包括这样的模板:

<div ng-init="viewTitle = 'translatable.title'" ng-include="'views/templates/view-title.html'"/>

事情正常。 但是,我无法从范围中传递变量。 如何实现两者?

您的主要问题是:

如何使用ng-init设置范围属性? 您需要先用ng-init读取scope属性,然后再用angular编写它。 您不能在ng-init中访问$ scope属性。 而是在控制器中设置$ scope属性(js代码)。

这些是您可能会遇到的其他问题:

这将起作用(请参阅下文),但是您可能会遇到典型的角度范围界定问题,有两种可能的解决方案:

始终遵循点规则( https://egghead.io/lessons/angularjs-the-dot ),将帮助您避免我们职业生涯中的许多范围界定问题

或使用“控制器为”语法( http://toddmotto.com/digging-into-angulars-controller-as-syntax/ )。

模板:

<h2 class="viewTitle">{{myScopeAttributeInParentScope | translate}}</h2> data: {{myScopeAttributeInParentScope}}  

与String一起使用:

<div ng-init="myScopeAttributeInParentScope=translatable.title'}" ng-include="'views/templates/view-title.html'"/>

与范围属性一起使用:

<div ng-include="'views/templates/view-title.html'"/>  
parent Scope: {{myScopeAttributeInParentScope}} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM