gpt4 book ai didi

Angular - controlValueAccessor 方法 writeValue() 未被调用

转载 作者:行者123 更新时间:2023-12-03 00:39:38 28 4
gpt4 key购买 nike

Angular 版本:6.1

我正在尝试使用 Kara Erickson 所称的 Composite ControlValueAccessor 来实现自定义控件,如演示文稿中所示 Angular Forms – Kara Erickson – AngularConnect 2017 。控件的值应设置为两个子输入值之一。

问题是 writeValue() 似乎仅在最初被调用,而不会在进一步的值更改时被调用。

这是一个stackblitz demo .

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;
};
}

最佳答案

writeValue 的目的是通知您的组件有关父组件的更改。您通常想要用它做的是将外部的更改绑定(bind)到局部变量。

为了向外界通知组件内部的更改,您需要调用 onChange 方法。您可以调用它,就像通常调用事件发射器一样。

我在这里整理了几个示例:https://www.tsmean.com/articles/angular/angular-control-value-accessor-example/

关于Angular - controlValueAccessor 方法 writeValue() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565659/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com