ngif保持假的,即使它的值是正确
问题描述:
我的代码如下所示:ngif保持假的,即使它的值是正确
component.ts
export class ViewScaleComponent implements OnInit {
private range$: Observable<number>;
constructor(private store: Store<AppState>) {}
ngOnInit() {
this.range$ = this.store.select(x => x.view.range);
}
}
component.html:
<div *ngIf="(range$ | async)">
here {{ range$ | async }}
</div>
股利留在评论DOM,一个角度的鬼魂。
如果我删除ngif
,则会出现div
,但数据插值不会提取任何内容(仅此处和下面的空格出现)。
如果ngOnInit()
我做的:
this.range$ = this.store.select(x => x.view.range).do(x => console.log(x));
console.log("on init " + this.range$);
然后我得到:
on init [object Object]
undefined
21
其中最后两个控制台日志来自同一行(do()
)。我想知道第一个undefined
是不是显示di
的原因。
值得一提的是,有时它工作(我看到了div
和预期正在更新的range$
值)。
任何想法?
PS:我读了angular 2 ngIf with observable?,但可观察的和async
的组合看起来很像我的。
答
你不需要的ngIf
这只是把{{ range$ | async}}
更新
range:any ;
ngOnInit() {
this.store.select(x => x.view.range).subscribe(x => this.range = x);
}
模板
<div *ngif="range">
here {{ range }}
</div>
请张贴正确的语法。我在这里看到'* ngif =“range $ | async)”'至少有两个错误 – yurzui
如果你提供了一个最小的再现 – yurzui
我很喜欢用Plunker @yurzui尝试,但是我很难模拟存储服务数据。我应该注意到我是初学者。 – gsamaras