【发布时间】:2019-01-05 00:47:49
【问题描述】:
Angular 版本:6.1
我正在尝试使用 Kara Erickson 所称的 Composite ControlValueAccessor 来实现自定义控件,如演示文稿Angular Forms – Kara Erickson – AngularConnect 2017 中所示。控件的值应设置为两个子输入值之一。
问题是writeValue() 似乎只在最初被调用,而不是在进一步的值更改时被调用。
form.html
<form #f="ngForm" (ngSubmit)="f.form.valid && Submit(f)">
<confirm-password name='password' ngModel #pwd='ngModel'></confirm-password>
<div>{{pwd.status}}</div>
</form>
确认密码.html
<div [formGroup]='pass'>
<label>Password:
<input type="password" formControlName='pwd1' (blur)='onTouched()'>
</label>
<div *ngIf="controlDir.touched && controlDir.control.errors?.required"
class="error">Password is required.</div>
<label>Please re-enter password:
<input type="password" formControlName='pwd2' (blur)='onTouched()'>
</label>
<div class='error' *ngIf='controlDir.touched && controlDir.control.errors?.notEqual &&
!controlDir.errors?.required'>Passwords do not match.</div>
</div>
确认密码.ts
import { Component, Self, OnInit, OnDestroy } from '@angular/core';
import { ControlValueAccessor, AbstractControl, NgControl,
ValidatorFn, Validators, FormGroup, FormControl } from '@angular/forms';
import { Subscription } from 'rxjs';
@Component({
selector: 'confirm-password',
templateUrl: './confirm-password.component.html',
styleUrls: ['./confirm-password.component.css']
})
export class ConfirmPasswordComponent implements ControlValueAccessor, OnInit, OnDestroy
{
password: string; // the value of the control should be set to this value
pass = new FormGroup({
pwd1: new FormControl(),
pwd2: new FormControl()
});
private onChange: (value: string) => void;
private onTouched: (value: string) => void;
private valueChanges: Subscription;
constructor(@Self() public controlDir: NgControl)
{
controlDir.valueAccessor = this;
}
ngOnInit()
{
const control = this.controlDir.control;
let myValidators = [
Validators.required,
equalValidator(this.pass.controls.pwd1,
this.pass.controls.pwd2)
]
// ovoid overwriting any existing validators
let validators = control.validator
? [control.validator, ...myValidators]
: [...myValidators];
control.setValidators(validators);
control.updateValueAndValidity();
}
writeValue(val: any)
{
/* whether everything inside of this method is commented out or not
doesn't seem to affect anything */
console.log(val);
//val && this.pass.setValue(val, {emitEvent: false});
this.password = val;
}
registerOnChange(fn: (val: any) => void)
{
this.valueChanges = this.pass.valueChanges.subscribe(fn);
}
registerOnTouched(fn: () => void)
{
this.onTouched = fn;
}
setDisabledState(disabled: boolean)
{
disabled ? this.pass.disable() : this.pass.enable();
}
ngOnDestroy()
{
this.valueChanges.unsubscribe();
}
}
export function equalValidator(el1, el2): ValidatorFn
{
return (ctrl: AbstractControl): {[key: string]: any} | null => {
const notEqual = el1.value !== el2.value;
return notEqual ? {"notEqual": true} : null;
};
}
【问题讨论】:
-
链接到 stackblitz 编辑器 plz
-
哎呀,没有意识到我链接到了错误的链接。现在应该可以工作了。
-
对我来说很好:stackblitz.com/edit/angular-ukowfl?file=src/app/… - 按下按钮
-
你改了吗?它对我来说根本不起作用,控制台中有几个错误..
标签: angular