Jey*_*ani 3 angular-material angular
我使用了 angular material("@angular/material": "7.1.0") 自动完成组件,然后我使用了表单控件而不是 ng 模型,现在的问题是,当我无法获得自动完成更改事件时组件正在加载。我在 ngOnInint 方法上将值设置为自动完成组件,当时我想根据自动完成值更改调用一个方法。
我在 stackblitz 创建了一个代码,这里是链接:https ://stackblitz.com/edit/angular-xmnmpy
Ant*_*bun 13
你必须使用optionSelected
事件<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onSelectionChange($event)" >
您必须在 html 中使用:
<form [formGroup]="myGroup">
<mat-form-field>
<input type="text"
matInput
[matAutocomplete]="auto"
formControlName="identifier">
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onSelectionChange($event)" >
<mat-option *ngFor="let animal of animals"
[value]="animal.name">
{{animal.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)
在你的 ts 中:
onSelectionChange(event){
console.log('onSelectionChange called', event.option.value);
}
Run Code Online (Sandbox Code Playgroud)
希望我对你有所帮助,如果你需要帮助,请随时问我!
好吧,首先,你ngOnInit
根本没有使用。
其次,ngModelChange
不适用于反应形式。
您必须像这样监听值的变化:
this.myGroup
.get('identifier').valueChanges.subscribe(value => console.log('Value changes'));
Run Code Online (Sandbox Code Playgroud)
这是通过反应形式聆听价值变化的标准方法。
这是您的StackBlitz。
归档时间: |
|
查看次数: |
12511 次 |
最近记录: |