【发布时间】:2018-10-17 18:19:32
【问题描述】:
在 Angular Material Design 6 中,删除了(更改)方法。 当用户更改选择时,我应该如何替换更改方法来执行组件中的代码?
【问题讨论】:
在 Angular Material Design 6 中,删除了(更改)方法。 当用户更改选择时,我应该如何替换更改方法来执行组件中的代码?
【问题讨论】:
如果您使用的是响应式表单,您可以像这样监听选择控件的更改..
this.form.get('mySelectControl').valueChanges.subscribe(value => { ... do stuff ... })
【讨论】:
.updateValueAndValidity,控件不要忘记传递{ emitEvent: false }以避免RangeError: Maximum call stack size exceeded。另一方面,感谢您的提示 (+1),它引导我找到了我需要的东西。
将其从 change 更改为 selectionChange。
<mat-select (change)="doSomething($event)">
现在
<mat-select (selectionChange)="doSomething($event)">
【讨论】:
(changeEventChange) 事件来检测更改事件何时发生变化。
selectionChangematerial.angular.io/components/select/api
对我来说 (selectionChange) 和建议的 (onSelectionChange) 不起作用,我没有使用 ReactiveForms。我最终做的是使用(valueChange) 事件,例如:
<mat-select (valueChange)="someFunction()">
这对我有用
【讨论】:
<mat-select placeholder="Select an option" [(ngModel)]="project.managerId" name="managerId" required (selectionChange)="fillComanager(project.managerId)"> <mat-option *ngFor="let manager of managers" [value]="manager.id"> {{ manager.name }} </mat-option> </mat-select>
为:
1) mat-select (selectionChange)="myFunction()" 工作在角度为:
sample.component.html
<mat-select placeholder="Select your option" [(ngModel)]="option" name="action"
(selectionChange)="onChange()">
<mat-option *ngFor="let option of actions" [value]="option">
{{option}}
</mat-option>
</mat-select>
sample.component.ts
actions=['A','B','C'];
onChange() {
//Do something
}
2) 简单的 html select (change)="myFunction()" 工作在 Angular 中:
sample.component.html
<select (change)="onChange()" [(ngModel)]="regObj.status">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
sample.component.ts
onChange() {
//Do something
}
【讨论】:
我今天遇到了 mat-option-group 的这个问题。 解决我问题的方法是在其他提供的 mat-select 事件中使用: 值变化
我在这里放了一些代码以供理解:
<mat-form-field >
<mat-label>Filter By</mat-label>
<mat-select panelClass="" #choosedValue (valueChange)="doSomething1(choosedValue.value)"> <!-- (valueChange)="doSomething1(choosedValue.value)" instead of (change) or other event-->
<mat-option >-- None --</mat-option>
<mat-optgroup *ngFor="let group of filterData" [label]="group.viewValue"
style = "background-color: #0c5460">
<mat-option *ngFor="let option of group.options" [value]="option.value">
{{option.viewValue}}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
垫版:
"@angular/material": "^6.4.7",
【讨论】:
对我来说...(click)="myFunction()"... 工作;上面什么都没有。
【讨论】: