[英]listen component for change @Input() value in angular 8
嗨,我有這個組件。 在這個組件中,我有@Input() userId: number[] = [];
用於將 id 列表發送到此組件。
我在其他組件中使用這個組件,例如我在news
組件中使用它:
<kt-user-post-list-select [userId]="noWriterIdList" (selectedUserId)="getSelectionUserList($event)">
</kt-user-post-list-select>
當我向服務器發送添加新聞的請求時,它返回給我的 id 列表: [1,2,3]
然后我必須將該 id 發送到帶有此[userId]="noWriterIdList"
的kt-user-post-list-select
[userId]="noWriterIdList"
,但我有問題:我需要將列表傳遞給該組件時,它會跟蹤更改並執行此功能:
validateUSerIsWriter(ids: number[]): void {
for (let id = 0; id < ids.length; id++) {
let user = this.users.find(x => x.userId = id);
if (user != null) {
user.isDeleted = true;
}
}
}
但它沒有任何工作。
我怎么解決這個問題 ???
有2種方式
在kt-user-post-list-select
組件中使用ngOnChanges
鈎子。 所以你可以監聽新的 Id 並執行函數validateUSerIsWriter
。 但請記住,這會導致性能下降。
使用Subject
訂閱 newIds,並執行函數validateUSerIsWriter
。 在這種情況下,您不需要@Input
裝飾器。 請參考這個簡單的例子https://stackblitz.com/edit/angular-subject-observable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.