如何更新嵌套的 observable ain angular 9?
how to update a nested observable ain angular 9?
我有一个要修改的可观察对象。
private userProposalConversatorAfterLogin = new BehaviorSubject<any>([]);
getUserProposalConversatorAfterLogin$ = this.userProposalConversatorAfterLogin.asObservable();
currentUserProposalConversatorAfterLoginValue():ParcelModel[] {
return this.userProposalConversatorAfterLogin.value;
}
setUserProposalConversatorAfterLogin(x) {
this.userProposalConversatorAfterLogin.next(x);
}
这是订阅的结果(this.getUserProposalConversatorAfterLogin$.suscribe(proposal))
{
id:1,
messages:[{id:'1', comments:'hello'}, {id=2, comments:'aaa'}, .....]
}
我想向消息数组添加一条新消息,例如:{id=3, comments:'oooo'} 和
当我再次订阅时,我想得到:
{
id:1,
messages:[{id:'1', comments:'hello'}, {id=2, comments:'aaa'}, {id=3, comments:'oooo'} .....]
}
我试了很多方法都没有成功
getUserProposalConversatorAfterLogin$.pipe(
switchMap(x => {
const new_array = (x.filter(x => x['id'] === proposal_identifiant))
console.log(new_array);
if (new_array && new_array.length >=1) {
new_array[0]['message_proposal_identifiant'].push(new_message);
console.log(new_array[0]['message_proposal_identifiant']);
}
return new_array
}),
share(),
distinctUntilChanged(),
)
当我再次订阅时,我没有看到新的值。我认为我们需要修改 userProposalConversatorAfterLogin,但如何修改?
我试过 this.userProposalConversatorAfterLogin.next(val)
但它只添加了一个新对象
提前谢谢你
当您使用 BehaviorSubject
时,您可以对其调用 getValue()
,它会为您提供当前值。并将当前值存储在变量中
例如:
const currentValue = this.userProposalConversatorAfterLogin.getValue();
// In your case current value is an array, so you can do below
currentValue.push( {id=3, comments:'oooo'} );
// and then call .next() with updated current value
this.userProposalConversatorAfterLogin.next( currentValue );
所以你会得到所有的三个对象
工作Stackblitz
我有一个要修改的可观察对象。
private userProposalConversatorAfterLogin = new BehaviorSubject<any>([]);
getUserProposalConversatorAfterLogin$ = this.userProposalConversatorAfterLogin.asObservable();
currentUserProposalConversatorAfterLoginValue():ParcelModel[] {
return this.userProposalConversatorAfterLogin.value;
}
setUserProposalConversatorAfterLogin(x) {
this.userProposalConversatorAfterLogin.next(x);
}
这是订阅的结果(this.getUserProposalConversatorAfterLogin$.suscribe(proposal))
{
id:1,
messages:[{id:'1', comments:'hello'}, {id=2, comments:'aaa'}, .....]
}
我想向消息数组添加一条新消息,例如:{id=3, comments:'oooo'} 和 当我再次订阅时,我想得到:
{
id:1,
messages:[{id:'1', comments:'hello'}, {id=2, comments:'aaa'}, {id=3, comments:'oooo'} .....]
}
我试了很多方法都没有成功
getUserProposalConversatorAfterLogin$.pipe(
switchMap(x => {
const new_array = (x.filter(x => x['id'] === proposal_identifiant))
console.log(new_array);
if (new_array && new_array.length >=1) {
new_array[0]['message_proposal_identifiant'].push(new_message);
console.log(new_array[0]['message_proposal_identifiant']);
}
return new_array
}),
share(),
distinctUntilChanged(),
)
当我再次订阅时,我没有看到新的值。我认为我们需要修改 userProposalConversatorAfterLogin,但如何修改?
我试过 this.userProposalConversatorAfterLogin.next(val)
但它只添加了一个新对象
提前谢谢你
当您使用 BehaviorSubject
时,您可以对其调用 getValue()
,它会为您提供当前值。并将当前值存储在变量中
例如:
const currentValue = this.userProposalConversatorAfterLogin.getValue();
// In your case current value is an array, so you can do below
currentValue.push( {id=3, comments:'oooo'} );
// and then call .next() with updated current value
this.userProposalConversatorAfterLogin.next( currentValue );
所以你会得到所有的三个对象