简体   繁体   English

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

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

相关问题 如何在 Angular 中传递参数 - How to pass argument in Angular 如何将String []传递给我的angular2 html模板 - How to pass a String[] to my angular2 html template 如何在有角项目的html组件之间传递字符串变量作为参数? - how can I pass a string variable as argument between the html components of an angular project? 如何将串联字符串作为参数传递给函数:Angular 2 - How to pass a concatenated string as a parameter to function: Angular 2 如何在 angular HTML Template 中 (click) = "{{ DynamicFunctionCall }}" 动态传值? - How to pass value dynamically in (click) = "{{ DynamicFunctionCall }}" in angular HTML Template? 如何将预先生成的数组按角度传递给模板中的组件? - How can I pass pre generated array to component in template in angular? 使用 FastAPI 渲染时将 Jinja2 模板作为 html 字符串传递? - Pass Jinja2 template as html string while rendering using FastAPI? 如何在Angular对象中使用for循环传递给由$ sce呈现的字符串 - How can I pass using a for-loop in an Angular object to a string that gets rendered by $sce 如何通过 HTML 将字符串传递到 ng-template 中? - How to pass a string into a ng-template via HTML? 如何将 django 模板中的参数传递给单击列表中的列表项的视图? - How to pass argument from django template to views on clicking list item from list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM