大佬教程收集整理的这篇文章主要介绍了角度材料 – 具有错误状态的自定义反应式控件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
定制text.component.html
<mat-form-field class="example-full-width"> <mat-SELEct [placeholder]="placeholder" #SELEct [formControl]="control"> <mat-option *ngFor="let food of foods" [value]="food"> {{fooD}} </mat-option> </mat-SELEct> <mat-error>This is required.</mat-error> </mat-form-field>
定制text.component.ts
import { Component,ViewChild,HostBinding,Input,ChangeDetectionStrategy,Optional,Self,Docheck,OnInit,NgZone } from '@angular/core'; import { ControlValueAccessor,NgControl,NgForm,FormGroupDirective,FormControlDirective,FormControlName,FormControl,FormBuilder } from '@angular/forms'; import { MatFormFieldControl,MatSELEct,CanupdateErrorState,ErrorStateMatcher } from '@angular/material'; @Component({ SELEctor: 'custom-text',templateUrl: './custom-text.component.html',styleUrls: [ './custom-text.component.scss' ],providers: [ { provide: MatFormFieldControl,useExisTing: CustomTextComponent } ],changeDetection: ChangeDetectionStrategy.onPush }) export class CustomTextComponent implements ControlValueAccessor,Docheck { @input() foods: String[]; @input() get errorStateMatcher(): ErrorStateMatcher { return this.SELEct.errorStateMatcher; } set errorStateMatcher(val) { this.SELEct.errorStateMatcher = val; } @input() get placeholder() { return this.SELEct.placeholder; } set placeholder(plh) { this.SELEct.placeholder = plh; this.stateChanges.next(); } @input() get value() { return this.SELEct.value; } set value(val) { this.SELEct.value = val; this.stateChanges.next(); } @ViewChild('SELEct') SELEct: MatSELEct; control: FormControl; constructor( @Optional() @Self() ngControl: NgControl,@Optional() private _controlName: FormControlName) { if (ngControl) { ngControl.valueAccessor = this; } } ngOnInit(): void { this.control = this._controlName.control; } ngDocheck(): void { this.SELEct.updateErrorState(); } writeValue(obj: any): void { this.value = obj; } registerOnChange(fn: any): void { this.SELEct.registerOnChange(fn); } registerOnTouched(fn: any): void { this.SELEct.registerOnTouched(fn); } setDisabledState?(isDisabled: Boolean): void { this.SELEct.setDisabledState(isDisabled); } }
app.component.html
<div style="text-align:center"> <form class="example-form" [formGroup]="myForm" (submit)="submitForm()"> <custom-text [foods]="[null,'burger','spaghetti','fries']" formControlName="SELEctedFood" [errorStateMatcher]="matcher"></custom-text> <button>Submit</button> </form> </div>
基本上,我将FormControlName实例注入自定义控件.
constructor( @Optional() private _controlName: FormControlName) { .... ngOnInit(): void { this.control = this._controlName.control; }
<mat-SELEct [placeholder]="placeholder" #SELEct [formControl]="control">
然后我在NgDocheck中调用this.SELEct.updateErrorState.
这是StackBlitz的链接:
https://stackblitz.com/edit/angular-c4ufpp
是否有更好或更标准的方法?
import { NG_VALIDATORS,NG_VALUE_ACCESSOR,Validator,Validators } from'@angular/forms'; ..other imports @Component({ ... providers: [ { provide: NG_VALUE_ACCESSOR,useExisTing: forWARDRef(() => YourComponent),multi: true },{ provide: NG_VALIDATORS,multi: true,} ] }) validate(c: AbstractControl): ValidationErrors | null { ... put your validation check in here and return null if it´s valid }
以上是大佬教程为你收集整理的角度材料 – 具有错误状态的自定义反应式控件全部内容,希望文章能够帮你解决角度材料 – 具有错误状态的自定义反应式控件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。