我的請求資料中有 2 個陣列。一個包含包含的產品,其他 - 排除。你不能同時使用兩者。所以我有這樣的HTML:
<input [formControl]="this.exclude$ ? excludedFormControl : includedFormControl>
<mat-slide-toggle (change)="excludeChange($event.checked)"></mat-slide-toggle>
這是我的 TS:
exclude$ = new BehaviorSubject<boolean>(false);
currentFilter = {
productsSection: {
includedIds: [],
excludedIds: [],
},
};
includedFormControl: FormControl = new FormControl(
this.currentFilter.productsSection.includedIds ? this.currentFilter.productsSection.includedIds : [],
);
excludedFormControl : FormControl = new FormControl(
this.currentFilter.productsSection.excludedIds? this.currentFilter.productsSection.excludedIds: [],
);
excludeChange(value: boolean): void {
this.filterForm.patchValue({
includedIds: [],
excludedIds: [],
});
this.exclude$.next(value);
}
showFormValue() {
console.log(this.filterForm.value);
}
問題:當我滑動切換時, excludeChange 被呼叫,但輸入仍然將資料寫入 includedIds,忽略滑塊。
uj5u.com熱心網友回復:
我認為您必須從exclude$.
<input [formControl]="excluded ? excludedFormControl : includedFormControl">
在 component.ts 檔案中,您必須定義excluded.
excluded = false;
this.exclude$.subscribe(res => {
this.excluded = res;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481458.html
下一篇:打字稿型別中的動態鍵
