Angular mat-select 组件的 patchValue
Angular patchValue of a mat-select component
在我的 Angular 7 应用程序中,我正在使用 mat-select 的组件 angular material 反应形式。
视图如下所示:
<mat-form-field class="col-md-3" color="warn">
<mat-select placeholder="Selectionner la boutique"
id="libelleShop"
[(value)]="selectedlibelleShopoValue"
ngDefaultControl
formControlName="libelleShop"
(selectionChange)="onShopSelectionChanged($event)">
<mat-option *ngFor="let shop of shopsList"
[value]="shop">
{{shop.storeName}}
</mat-option>
</mat-select>
MD数据如下:
shopsList= [
{
'edoId': '2119',
'storeName': 'AIX LES BAINS'
},
{
'edoId': '2123',
'storeName': 'ANNEMASSE'
},
{
'edoId': '2460',
'storeName': 'ALENCON'
},
{
'edoId': '2478',
'storeName': 'Grand Evreux Carrefour'
},
{
'edoId': '2632',
'storeName': 'DESTRELAND'
}
]
第一次加载后,选项在 select 下拉列表中成功读取,我有一个按钮用于在单击时强制 mat-select 的值。
我已经试过了:
onClick(){
let shopObjToDisplay = {};
shopObjToDisplay['edoId'] = '2460';
shopObjToDisplay['storeName'] = 'ALENCON';
this.myForm.patchValue({'libelleShop': shopObjToDisplay });
}
不幸的是,我的数据似乎没有设置。
有什么想法吗??
您需要在 mat-select 输入上使用 compareWith 函数。
示例:
<select [compareWith]="compareFn" [formControl]="selectedCountriesControl">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>
compareFn(c1: Country, c2: Country): boolean {
return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
使用这个:-
onClick(){
let shopObjToDisplay = {};
shopObjToDisplay['edoId'] = '2460';
shopObjToDisplay['storeName'] = 'ALENCON';
this.myForm.patchValue({
libelleShop: shopObjToDisplay
});
}
一个老问题...你可以用 triggerValue
和 mat-select 和补丁值来实现这个。
<mat-form-field class="full-width" appearance="fill">
<mat-select #level formControlName="level">
<mat-option value="Native Speaker">Native Speaker</mat-option>
<mat-option value="Highly Proficient">Highly Proficient</mat-option>
<mat-option value="Very Good Command">Very Good Command</mat-option>
</mat-select>
</mat-form-field>
通过以上,您现在可以获取模板中的值了。
<div *ngIf="level.triggerValue && (level.triggerValue).length; else notSpecifiedDate" class="text-muted"
fxFlex="100">{{level.triggerValue}}</div>
<ng-template #notSpecifiedDate>
<div class="text-muted" fxFlex="100">
{{'Not Specified' | translate}}
</div>
</ng-template>
在我的 Angular 7 应用程序中,我正在使用 mat-select 的组件 angular material 反应形式。
视图如下所示:
<mat-form-field class="col-md-3" color="warn">
<mat-select placeholder="Selectionner la boutique"
id="libelleShop"
[(value)]="selectedlibelleShopoValue"
ngDefaultControl
formControlName="libelleShop"
(selectionChange)="onShopSelectionChanged($event)">
<mat-option *ngFor="let shop of shopsList"
[value]="shop">
{{shop.storeName}}
</mat-option>
</mat-select>
MD数据如下:
shopsList= [
{
'edoId': '2119',
'storeName': 'AIX LES BAINS'
},
{
'edoId': '2123',
'storeName': 'ANNEMASSE'
},
{
'edoId': '2460',
'storeName': 'ALENCON'
},
{
'edoId': '2478',
'storeName': 'Grand Evreux Carrefour'
},
{
'edoId': '2632',
'storeName': 'DESTRELAND'
}
]
第一次加载后,选项在 select 下拉列表中成功读取,我有一个按钮用于在单击时强制 mat-select 的值。
我已经试过了:
onClick(){
let shopObjToDisplay = {};
shopObjToDisplay['edoId'] = '2460';
shopObjToDisplay['storeName'] = 'ALENCON';
this.myForm.patchValue({'libelleShop': shopObjToDisplay });
}
不幸的是,我的数据似乎没有设置。
有什么想法吗??
您需要在 mat-select 输入上使用 compareWith 函数。
示例:
<select [compareWith]="compareFn" [formControl]="selectedCountriesControl">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>
compareFn(c1: Country, c2: Country): boolean {
return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
使用这个:-
onClick(){
let shopObjToDisplay = {};
shopObjToDisplay['edoId'] = '2460';
shopObjToDisplay['storeName'] = 'ALENCON';
this.myForm.patchValue({
libelleShop: shopObjToDisplay
});
}
一个老问题...你可以用 triggerValue
和 mat-select 和补丁值来实现这个。
<mat-form-field class="full-width" appearance="fill">
<mat-select #level formControlName="level">
<mat-option value="Native Speaker">Native Speaker</mat-option>
<mat-option value="Highly Proficient">Highly Proficient</mat-option>
<mat-option value="Very Good Command">Very Good Command</mat-option>
</mat-select>
</mat-form-field>
通过以上,您现在可以获取模板中的值了。
<div *ngIf="level.triggerValue && (level.triggerValue).length; else notSpecifiedDate" class="text-muted"
fxFlex="100">{{level.triggerValue}}</div>
<ng-template #notSpecifiedDate>
<div class="text-muted" fxFlex="100">
{{'Not Specified' | translate}}
</div>
</ng-template>