可重用组件在多次使用时在组件中发出相同的值
我创建了 Angular 可重用材质多选选项,如下所示。当我在 app.ts 中多次使用这个可重用组件时,两个 mat-reusable-select 值都会互相覆盖。如何获取 app.ts 中的两个值?我错过了什么?
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);
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有两种方法。
1- 为每个
创建单独的函数2- 修改当前函数
onSelectionChange(event)
有两个参数,一个用于事件
,另一个用于源src
。我更喜欢第二种解决方案,如下所示:
在模板中,修改事件绑定:
这样您就可以获得两个属性
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 theevent
and another one for the sourcesrc
.I prefer the 2nd solution and it goes as follows:
in your template, modify the event binding:
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.