當我檢查checkbox時,undefined當我取消選中獲取我之前檢查的值時,我得到了。如何解決這個問題?
<div *ngSwitchCase="'multiselect'" [ngClass]="{'mandate': field.required}" >
<mat-form-field appearance="outline" >
<mat-label>{{field.placeHolder}}</mat-label>
<mat-select [formControlName]="field.controlName" multiple [(ngModel)]="countrySelected" >
<mat-option *ngFor="let option of field.options" [value]="option"
(onSelectionChange)="countriesUpdate($event)">
{{option.label}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
console.log(this.countrySelected); undefined支票上
如何獲得當前值?
uj5u.com熱心網友回復:
onSelectionChange 在設定表單值之前觸發,所以你會得到最后設定的值,有兩種方法可以解決這個問題
- 在 mat-select 上監聽 selectionChange 而不是在 mat-option 上監聽 onSelectionChange
- 訂閱表單控制元件值變化事件
<mat-select
[formControlName]="field.controlName"
multiple
[(ngModel)]="countrySelected"
(selectionChange)="countriesUpdate($event)"
>
<mat-option *ngFor="let option of field.options" [value]="option.label">
{{ option.label }}
</mat-option>
</mat-select>
ngOnInit() {
this.formGroup
.get('formControlName')
.valueChanges.subscribe((selectValue) => console.log(selectValue));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/535085.html
標籤:有角度的角度材料
上一篇:登錄后限制對選單的訪問
