英文:
angular array observable does not get updated
问题
HTML中的内容无需翻译,但是你提供的HTML代码中包含一些HTML实体编码,需要还原成正常的HTML标记。以下是已翻译和修复的HTML代码:
<small>{{(selectedLangs$ | async)}}</small>
请注意,HTML代码中的特殊字符已被还原,现在可以正确显示。如果数组的值发生更改,HTML中的小标签内容应该会相应更新。
英文:
I have ts code as below
selectedLangs : string[] = []
selectedLangs$: Observable<string[]> = of(this.selectedLangs);
ngOnInit(){
this.form.get( 'languages' ).valueChanges
.pipe(
takeUntil( this._unSubscribeAll ),
tap( langs => {
this.selectedLangs = [];
if( langs ) {
langs.forEach( ( lang: IOption ) => {
if( this.selectedLangs.find( f => f === lang ) === undefined ) {
this.selectedLangs.push( lang )
}
} )
}
} )
)
.subscribe();
}
html
<small>{{(selectedLangs$ | async)}}</small>
Should my html be updated if I am changing the value of the array?
答案1
得分: 1
这是您想要做的吗?
selectedLangs$: BehaviorSubject<string[]> = new BehaviorSubject([]);
ngOnInit() {
this.form.get('languages').valueChanges.pipe(
takeUntil(this._unSubscribeAll),
tap((langs: IOption[]) => {
const uniqueLangs = [...new Set(langs)];
this.selectedLangs$.next(uniqueLangs);
})
).subscribe();
}
英文:
Is this what you want to do?
selectedLangs$: BehaviorSubject<string[]> = new BehaviorSubject([]);
ngOnInit() {
this.form.get('languages').valueChanges.pipe(
takeUntil(this._unSubscribeAll),
tap((langs: IOption[]) => {
const uniqueLangs = [...new Set(langs)];
this.selectedLangs$.next(uniqueLangs);
})
).subscribe();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论