可重用组件在多次使用时在组件中发出相同的值

发布于 2025-01-13 14:33:47 字数 2180 浏览 0 评论 0原文

我创建了 Angular 可重用材质多选选项,如下所示。当我在 app.ts 中多次使用这个可重用组件时,两个 mat-reusable-select 值都会互相覆盖。如何获取 app.ts 中的两个值?我错过了什么?

ma​​t-reusable-select.html

<mat-form-field appearance="none"
                floatLabel="never">
    <mat-select [formControl]="multiSelectheckboxFormControl"
                (selectionChange)="onSelectionChange($event)"
                placeholder="{{placeholder}}"
                #selection
                multiple>
        <mat-select-trigger class="mat-select-trigger">
            {{selection.value ? selection.value.length : ''}}
            <span *ngIf="multiSelectheckboxFormControl.value?.length >= 1">
                {{placeholder}}
            </span>
        </mat-select-trigger>
        <mat-option *ngFor="let item of data"
                    color="primary"
                    [value]="item.name">{{item.name}}</mat-option>
    </mat-select>
</mat-form-field>

ma​​t-reusable-select.ts

  @Input() placeholder: string;
  @Input() data: any;
  @Output() selectionChange = new EventEmitter<MatSelectChange>();

  multiSelectheckboxFormControl = new FormControl();

  onSelectionChange(event: MatSelectChange): any {
    this.selectionChange.emit(event.value);
  }

app.html

<form [formGroup]="searchForm" (ngSubmit)="onSubmit()">
  <mat-reusable-select [data]="products | async"
  [placeholder]="'Products'"
  (selectionChange)="onSelectionChange($event)">
  </mat-reusable-select>

  <mat-reusable-select [data]="sales | async"
  [placeholder]="'Sales'"
  (selectionChange)="onSelectionChange($event)">
  </mat-reusable-select>

  <button type="submit">submit</button>
</form>

app.ts

onSelectionChange(event: string[]) {
  // output: ['prod1', 'product]
  console.log(event);
}

I have created Angular reusable material multi select options as in below. When I used this reusable component in app.ts multiple times, both mat-reusable-select values are overwriting each other. How I can get both values in app.ts? What I have missed?

mat-reusable-select.html

<mat-form-field appearance="none"
                floatLabel="never">
    <mat-select [formControl]="multiSelectheckboxFormControl"
                (selectionChange)="onSelectionChange($event)"
                placeholder="{{placeholder}}"
                #selection
                multiple>
        <mat-select-trigger class="mat-select-trigger">
            {{selection.value ? selection.value.length : ''}}
            <span *ngIf="multiSelectheckboxFormControl.value?.length >= 1">
                {{placeholder}}
            </span>
        </mat-select-trigger>
        <mat-option *ngFor="let item of data"
                    color="primary"
                    [value]="item.name">{{item.name}}</mat-option>
    </mat-select>
</mat-form-field>

mat-reusable-select.ts

  @Input() placeholder: string;
  @Input() data: any;
  @Output() selectionChange = new EventEmitter<MatSelectChange>();

  multiSelectheckboxFormControl = new FormControl();

  onSelectionChange(event: MatSelectChange): any {
    this.selectionChange.emit(event.value);
  }

app.html

<form [formGroup]="searchForm" (ngSubmit)="onSubmit()">
  <mat-reusable-select [data]="products | async"
  [placeholder]="'Products'"
  (selectionChange)="onSelectionChange($event)">
  </mat-reusable-select>

  <mat-reusable-select [data]="sales | async"
  [placeholder]="'Sales'"
  (selectionChange)="onSelectionChange($event)">
  </mat-reusable-select>

  <button type="submit">submit</button>
</form>

app.ts

onSelectionChange(event: string[]) {
  // output: ['prod1', 'product]
  console.log(event);
}

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

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

发布评论

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

评论(1

北城半夏 2025-01-20 14:33:47

你有两种方法。

1- 为每个 创建单独的函数

2- 修改当前函数 onSelectionChange(event)有两个参数,一个用于事件,另一个用于源src

我更喜欢第二种解决方案,如下所示:

onSelectionChange(event: string[], src: string) {
   if (src === 'firstReusableSelect') {
      this.firstReusableSelection = event;
   } else if (src === 'secondReusableSelect') {
      this.secondReusableSelection = event;
   }
}

在模板中,修改事件绑定:

<mat-reusable-select [data]="products | async"
   [placeholder]="'Products'"
   (selectionChange)="onSelectionChange($event, 'firstReusableSelection')">
</mat-reusable-select>

<mat-reusable-select [data]="sales | async"
   [placeholder]="'Sales'"
   (selectionChange)="onSelectionChange($event, 'secondReusableSelection')">
</mat-reusable-select>

这样您就可以获得两个属性 firstReusableSelection & secondReusableSelection 包含两个可重用组件中的选择,而不会相互覆盖。

我希望这就是您正在寻找的。

you have two ways.

1- to make separate function for each <mat-reusable-select></mat-reusable-select>

2- to modify the current function onSelectionChange(event) to have two parameters, one for the event and another one for the source src.

I prefer the 2nd solution and it goes as follows:

onSelectionChange(event: string[], src: string) {
   if (src === 'firstReusableSelect') {
      this.firstReusableSelection = event;
   } else if (src === 'secondReusableSelect') {
      this.secondReusableSelection = event;
   }
}

in your template, modify the event binding:

<mat-reusable-select [data]="products | async"
   [placeholder]="'Products'"
   (selectionChange)="onSelectionChange($event, 'firstReusableSelection')">
</mat-reusable-select>

<mat-reusable-select [data]="sales | async"
   [placeholder]="'Sales'"
   (selectionChange)="onSelectionChange($event, 'secondReusableSelection')">
</mat-reusable-select>

and this way you get two properties firstReusableSelection & secondReusableSelection containing the selection from both reusable components without overwriting each other.

I hope this is what you are looking for.

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