[英]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.