Ale*_*dro 10 angular-material angular
在我的角度5应用程序中,我有一些matAutocomplete,但我想强制选择一些建议,所以我遵循这种方法: stackblitz 但由于某种原因,在一个案例中我有一个问题:
无法在CustomerDetailComponent.ngAfterViewInit的CustomerDetailComponent.countryClosingActions(customer-detail.component.ts:199)中读取未定义的属性'panelClosingActions'
我有多个matAutocomplete,但只有这个有问题.(关于这个方法的信息在这里是github
HTML
<mat-form-field>
<input matInput #nation placeholder="{{'customer.detail.labels.country'
| translate }}" required [matAutocomplete]="tdAuto" name="country"
#count="ngModel" [(ngModel)]="selected.country"
(ngModelChange)="searchCountry($event)">
<mat-autocomplete #tdAuto="matAutocomplete" [displayWith]="displayFn">
<mat-option (onSelectionChange)="setCountry(country)" *ngFor="let country of countries" [value]="country">
<div class="row">
<img src="assets/img/flags24/{{country.alpha2Code | lowercase}}.png" />
<span>{{country.name}} ({{country.alpha2Code}})</span>
</div>
</mat-option>
</mat-autocomplete>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
零件
@ViewChild('nation', { read: MatAutocompleteTrigger }) trigger: MatAutocompleteTrigger;
subscription: Subscription;
ngAfterViewInit() {
this.countryClosingActions();
}
private countryClosingActions(): void {
if (this.subscription && !this.subscription.closed) {
this.subscription.unsubscribe();
}
this.subscription = this.trigger.panelClosingActions
.subscribe(e => {
console.log('closing')
if (!e || !e.source) {
this.selected.country = null;
this.selfCountry = null;
}
},
err => this.countryClosingActions(),
() => this.countryClosingActions());
}
Run Code Online (Sandbox Code Playgroud)
使用blur事件和matAutocomplete输出事件(optionSelected),我们可以强制用户选择选项.
<mat-form-field class="example-full-width">
<input type="text" placeholder="Country*" matInput formControlName="country" [matAutocomplete]="countryAutoList" (blur)="checkCountry()">
<mat-autocomplete autoActiveFirstOption #countryAutoList="matAutocomplete" (optionSelected)="countryClick($event)">
<mat-option *ngFor="let item of countryList" [value]="item.Name">{{item.Name}}</mat-option>
</mat-autocomplete>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
ts文件功能
countryClick(event: any) {
this.selectedCountry = event.option.value;
}
checkCountry() {
if (!this.selectedCountry || this.selectedCountry !== this.signatureFormGroup.controls['country'].value) {
this.signatureFormGroup.controls['country'].setValue(null);
this.selectedCountry = '';
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
从 Angular Material 16.2.1 开始,您可以在 mat-autocomplete 上使用 requireSelection 指令:
模板中的这个:
<form class="example-form">
<mat-form-field class="example-full-width">
<mat-label>Number</mat-label>
<input #input
type="text"
placeholder="Pick one"
matInput
[formControl]="myControl"
[matAutocomplete]="auto"
(input)="filter()"
(focus)="filter()">
<mat-autocomplete requireSelection #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)
课堂上是这样的:
@ViewChild('input') input: ElementRef<HTMLInputElement>;
myControl = new FormControl('');
options: string[] = ['One', 'Two', 'Three', 'Four', 'Five'];
filteredOptions: string[];
constructor() {
this.filteredOptions = this.options.slice();
}
filter(): void {
const filterValue = this.input.nativeElement.value.toLowerCase();
this.filteredOptions = this.options.filter(o => o.toLowerCase().includes(filterValue));
}
Run Code Online (Sandbox Code Playgroud)
他们在网站上给出的示例使用反应式表单方法,但在您的情况下,您似乎倾向于模板驱动方法,但您没有使用表单句点。
所以你可以像现在一样直接访问 dom 元素。
创建对您的输入的本地引用,也许将其称为 autoComplInput
在组件文件中,您需要导入ElementRef并查看Child
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
Run Code Online (Sandbox Code Playgroud)
在组件内部,导入 ViewChild,并将其声明为 ElementRef 类型
@ViewChild('autoComplInput') autoComplInput: ElementRef;
Run Code Online (Sandbox Code Playgroud)
然后在初始化的某个时刻,只需分配引用的元素值
ngOnInit() {
this.autoComplInput.nativeElement.value = this.countries[0];
}
Run Code Online (Sandbox Code Playgroud)
演示基于 Angular Material 2 的示例,因为您没有提供所需的所有信息。
在你的html中
<mat-form-field>
<input matInput
placeholder="{{'country'}}"
required
[matAutocomplete]="tdAuto"
name="country"
#autoComplInput
(ngModelChange)="searchCountry($event)">
<mat-autocomplete #tdAuto="matAutocomplete" >
<mat-option *ngFor="let country of countries" [value]="country">
<div class="row">
<span>{{country}} </span>
</div>
</mat-option>
</mat-autocomplete>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
在你的组件中
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
@Component({
selector: '<...>',
templateUrl: '<...>',
styleUrls: ['<...>'],
})
export class <...> implements OnInit {
@ViewChild('autoComplInput') autoComplInput: ElementRef;
countries = [
'Alabama',
'Alaska',
'Arizona',
'Arkansas',
'California',
'Colorado',
'Connecticut',
'Delaware',
'Florida',
'Georgia',
'Hawaii',
'Idaho',
'Illinois',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'North Carolina',
'North Dakota',
'Ohio',
'Oklahoma',
'Oregon',
'Pennsylvania',
'Rhode Island',
'South Carolina',
'South Dakota',
'Tennessee',
'Texas',
'Utah',
'Vermont',
'Virginia',
'Washington',
'West Virginia',
'Wisconsin',
'Wyoming',
];
constructor( ) {}
ngOnInit() {
this.autoComplInput.nativeElement.value = this.countries[0];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10815 次 |
| 最近记录: |