[英]Angular 6, access component variable inside ngFor scope
How can I access a components global variable inside a ngFor loop scope (which is in the html template of the component). 如何在ngFor循环范围内(位于组件的html模板中)访问组件的全局变量。
Please don't tell me this isn't possible, I'm disappointed enough with Angular already. 请不要告诉我这是不可能的,我已经对Angular感到非常失望。
<ng-container *ngFor="let option of options">
<label class="{{globalVariable}}">{{option}}</label>
</ng-container>
This doesn't work. 这行不通。 How can I use 'globalVariable' inside the ngFor scope?
如何在ngFor范围内使用“ globalVariable”?
The 'globalVariable' is define in the component like so: “ globalVariable”在组件中的定义如下:
@Input() globalVariable: 'someClass';
Use ngClass directive. 使用ngClass指令。
<p [ngClass]="red">
Start editing to see some magic happen :)
</p>
Pass the parameter globalVariable
as like (Note the single quote): 像这样传递参数
globalVariable
(注意单引号):
<app-temp [globalVariable]="'globalVariable'"></app-temp>
Then it will work as you are expecting: 然后它将按您期望的那样工作:
<ng-container *ngFor="let option of options">
<div class="{{globalVariable}}">{{option}}</div>
</ng-container>`
You have defined @Input() globalVariable: string;
您已经定义了
@Input() globalVariable: string;
. 。 So I believe you are passing that
globalVariable
as input from parent component like this : 因此,我相信您正在传递
globalVariable
作为来自父组件的输入,如下所示:
<parent-comp [globalVariable]="globalVariable"></parent-comp>
In above line there should be globalVariable
defined in your parent component ts file. 在上面的行中,您的父组件ts文件中应该定义了
globalVariable
。
Rest of your code looks fine to me : 您的其余代码对我来说似乎很好:
<ng-container *ngFor="let option of options">
<label class="{{globalVariable}}">{{option}}</label>
</ng-container>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.