这是代码示例:
电影组件
@Component(...)
class FilmsComponent implements OnInit {
@Input() films: Film[];
public addButtonShow$: Observable<bool>;
public ngOnInit(): void {
this.addButtonShow$ = of(this.films)
.pipe(
map(films => !!films.length)
)
}
}
Run Code Online (Sandbox Code Playgroud)
films.component.html
<film *ngFor="//iterating by filmsCollection"></film>
<button *ngIf="addButtonShow$ | async">Add new film</button>
Run Code Online (Sandbox Code Playgroud)
我只想在电影收藏集不为空的情况下显示按钮,但是当OnChanges钩触发的addButtonShow $不是时,我才显示按钮。
如何观察我的电影收藏的变化并更改“添加按钮”的可见性。(不使用ngOnChanges和更改showAddButton标志)