【发布时间】:2018-07-09 19:42:33
【问题描述】:
我试图在我的 html 中以角度 4+ 分配一个变量。这可能吗?
我试图实现的是将比较分配给一个变量,所以我不必在所有情况下都保持相同。
这是我想要实现的示例:
<mat-list-item role="list" *ngFor="let o of examles;" role="listitem">
<span *ngIf="o.type == 'EXAMPLE_TYPE'"> some text</span>
// here more divs
<span *ngIf="o.type == 'EXAMPLE_TYPE'"> other text</span>
// more divs...
<span *ngIf="o.type == 'EXAMPLE_TYPE'"> last text</span>
</mat-list-item>
所以,我的问题是,有没有办法声明类似的东西?
<div #isExampleType="o.type == 'EXAMPLE_TYPE'" >
然后在 *ngIf="isExampleType" 中使用它...
【问题讨论】:
-
你可以在检索数据的逻辑上使用 .map 函数来做到这一点。 http.get(...).map(o => {o.type == 'EXAMPLE_TYPE';return o;})
-
@LeonardoNeninger 是的,我知道,但我想知道我是否可以在 de html 中做到这一点
-
我不知道你为什么要这样做。我认为可以帮助您的另一种方法是创建一个接收“o”变量的指令,您可以从控制器中将其作为 ViewChild 引用
-
相关问题(不是完全欺骗),stackoverflow.com/questions/38582293/…
-
来自the Angular documentation:
A template reference variable is often a reference to a DOM element within a template. It can also be a reference to an Angular component or directive or a web component.
标签: javascript angular angular-directive