我有以下模板:
<div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = length">
Here is the length of my ngFor : {{l}}
</div>
Run Code Online (Sandbox Code Playgroud)
不幸的是,ngFor中不存在长度.我如何解决这个问题,以便在我的ngFor中提供长度?
我正在使用类似于angular2 heros教程的文本搜索功能,但是当用户没有搜索时我很难隐藏div.我的组件中有以下代码:
searchLocations: Observable<Location[]>;
private searchTerms = new Subject<string>();
// ...
ngOnInit(): void {
// ...
this.searchLocations = this.searchTerms
.debounceTime(300)
.distinctUntilChanged()
.switchMap(term => term
? this.locationSearchService.search(term)
: Observable.of<Location[]>([]))
.catch(error => {
console.error(error);
return Observable.of<Location[]>([]);
});
}
Run Code Online (Sandbox Code Playgroud)
然后在我的HTML中,我显示用户搜索的结果
<div class="results">
<div *ngFor="let loc of searchLocations | async" class="search-result">
{{ loc.name }}, {{ loc.adminName1 }}, {{ loc.countryCode }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是使用我当前的代码,resultsdiv 始终存在.如何检查可观察流何时为空?我试过.count()和.length(),但是这两种也返回观测....我想我需要做的是这样的:
<div class="results" *ngIf="???">
<!-- ... -->
</div>
Run Code Online (Sandbox Code Playgroud)