基于表达的角结合形式结合形式

发布于 2025-02-02 11:30:58 字数 1095 浏览 4 评论 0原文

我的请求数据中有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);
}

问题:当我滑动切换时,dubludechange被调用,但是输入仍然将数据写入纳入incruffeids中,忽略了滑块。

I have 2 arrays in my request data. One contains included products, other - excluded. You can't use both at the same time. So i have such HTML:

<input [formControl]="this.exclude$ ? excludedFormControl : includedFormControl>
<mat-slide-toggle (change)="excludeChange($event.checked)"></mat-slide-toggle>

Here is my 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);
}

Problem: When i slide the toggle the excludeChange gets called, but the input still writed the data into the includedIds, ignoring the slider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2025-02-09 11:30:58

我认为您必须从排除$订阅。

<input [formControl]="excluded ? excludedFormControl : includedFormControl">

在component.ts文件中,您必须定义排除

excluded = false;
this.exclude$.subscribe(res => {
  this.excluded = res;
});

I think you have to subscribe from the exclude$.

<input [formControl]="excluded ? excludedFormControl : includedFormControl">

In component.ts file, you have to define excluded.

excluded = false;
this.exclude$.subscribe(res => {
  this.excluded = res;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文