@Component({
selector: 'app-validation',
template: `
<form [formGroup]="form" (ngSubmit)="_submitForm(form)">
<input type="email" formControlName="email">
<input type="text" formControlName="nickname">
<button type="submit" [disabled]="form.invalid">Submit</button>
</form>
`
})
export class UserEditComponent {
constructor(private fb: FormBuilder, private route: ActivatedRoute) {}
ngOnInit() {
this.form = this.fb.group({
email: ['', Validators.compose([Validators.required, Validators.email])],
nickname: ['', [Validators.required]]
});
this.route.params
.switchMap((params: Params) => loadUser(+params['id']))
.subscribe(data => {
// Updating value
});
}
loadUser() {
return Observable.of({ email: 'xx@xx.com', nickname: 'cipchk' }).delay(1000);
}
_submitForm({ value }) {
// Save value
}
}
this.form = this.fb.group({
email: ['', Validators.compose([Validators.required, Validators.email])],
nickname: ['', [Validators.required]]
});
this.form.patchValue({ nickname: 'cipchk' });
patchValue(value: {[key: string]: any}, options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
Object.keys(value).forEach(name => {
if (this.controls[name]) {
this.controls[name].patchValue(value[name], {onlySelf: true, emitEvent: options.emitEvent});
}
});
this.updateValueAndValidity(options);
}
Object.keys({ nickname: 'cipchk' }).forEach(name => {
console.log(name);
});
// [ 'nickname' ]
this.controls[name].patchValue(value[name], {onlySelf: true, emitEvent: options.emitEvent});
setValue(value: {[key: string]: any}, options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
this._checkAllValuesPresent(value);
Object.keys(value).forEach(name => {
this._throwIfControlMissing(name);
this.controls[name].setValue(value[name], {onlySelf: true, emitEvent: options.emitEvent});
});
this.updateValueAndValidity(options);
}
_throwIfControlMissing(name: string): void {
if (!Object.keys(this.controls).length) {
throw new Error(`
There are no form controls registered with this group yet. If you're using ngModel,
you may want to check next tick (e.g. use setTimeout).
`);
}
if (!this.controls[name]) {
throw new Error(`Cannot find form control with name: ${name}.`);
}
}
reset(formState: any = null, options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
this._applyFormState(formState);
this.markAsPristine(options);
this.markAsUntouched(options);
this.setValue(this._value, options);
}
<button (click)="form.reset({ nickname: 'xx' })">Reset</button>
// Updating value
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有