I have made an Observable out of an array and i have subscribed to it.(我已经从数组中制作了一个Observable,我已经订阅了它。)
whenever I push items to an array, the list is updated in View, but when I do a filter function on that array it is not updated?(每当我将项目推送到数组时,列表都会在View中更新,但是当我对该数组执行过滤功能时,列表是否不会更新?)
The array is a property of a class, it is reassigned on doing array.filter()(数组是类的属性,在执行array.filter()时将其重新分配)
addUser(user) {
const newUsers = JSON.parse(window.localStorage.getItem('users'));
this.users.push(user);
newUsers.push(user);
window.localStorage.setItem('users', JSON.stringify(newUsers));
}
subscribeUsers(): Observable<any> {
this.users = JSON.parse(window.localStorage.getItem('users'));
return of(this.users);
}
searchUser(key) {
let name;
if (key === '') {
this.users = JSON.parse(window.localStorage.getItem('users'));
return;
} else {
this.users = this.users.filter(item => {
name = `${item.name.first} ${item.name.last}`;
if (name.includes(key)) {
return true;
} else {
return false;
}
});
}
console.log(this.users);
}
when addUser() is called, the subscriber is updated, list update in view, but when searchUser() is called, the subscriber is not updated, the list stays same.(调用addUser()时,将更新订阅者,并在视图中更新列表,但是当调用searchUser()时,不更新订阅者,列表将保持不变。) the search is working fine.(搜索工作正常。)
ask by River CR Phoenix translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…