0
在我管的文件我有這樣的代碼使用具有不確定的啓動一個管道值
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'search'
})
export class SearchPipe implements PipeTransform {
transform(service: any, term: any): any {
if (term=== undefined) return service.filter(function(service){
return service.product_name.toLowerCase().includes("rustic".toLowerCase());
})
return service.filter(function(service){
return service.product_name.toLowerCase().includes(term.toLowerCase());
})
}
}
我在這個模板致電上述管:
<div class="searchresult" *ngFor="let service of services |search:term ">
<div [routerLink]="['/searchresult',service.product_name]" class="searchitem">ID : {{ service.product_name }}</div><br>
</div>
如何知道service
還沒有準備好。因爲在我的應用程序中,我想從遠程rest API搜索數據(在我的VPS託管中)。目前我需要等待大約30秒,然後鍵入要傳遞給管道的關鍵字。如果我在30秒之前輸入它,大多數搜索將失敗,並返回Cannot read property 'filter' of undefined
。這是因爲其餘API尚未加載到服務中。如何知道我的服務尚未加載,所以我可以保持搜索輸入要禁止,直到服務準備使用...
每個幫助將是非常尊重....
謝謝,你節省了時間...這個工程 –