如何在模板中设置上下文变量

时间:2017-06-05 00:30:35

标签: angular

我希望我的页面依赖于从Observable发出的模型对象。如果对象是列表,我会使用

<div ngFor="let currentListItem of myObservable | async" >

然而,ngFor不适用,因为我没有模型列表,只有一个。有什么像

<div ngContext="let currentItem of myObservable |async" > 

这将允许我访问currentItem的属性,如

<div ngContext="let currentItem of myObservable |async" > 
     <label>{{currentItem.name}}
</div>

1 个答案:

答案 0 :(得分:1)

您可以在Angular 4中使用as关键字*ngIf*

<div *ngIf="myObservable | async as currentItem">
     <label>The current item's name is {{currentItem.name}}</label>
</div>
  

我想它可以奏效,但似乎不合情理。我不需要IF,它应该始终显示......

嗯,这是你想要的唯一方法。 *ngIf没有任何伤害;它会阻止我添加的文本(当前项目的名称是)从显示到可观察的发出,这可能是你想要的。