、それを使用していない私は、公式サンプルとNG2反応フォームを学んでいます:onValueChanged(データ:はなぜonvalueChangedに反応型のサンプルパスデータを行いますが
https://angular.io/docs/ts/latest/cookbook/form-validation.html#!#live-example
は、定義されたメソッドを見たことがありますか? :any){
データは渡される可能性がありますが、メソッド内で使用されることはありませんか?
なぜそれ以上の行を購読するのですか? =>
this.heroForm.valueChanges
.subscribe(data => this.onValueChanged(data));
誰も私にこの奇妙なコードを教えてもらえますか?
ngOnInit(): void {
this.buildForm();
}
buildForm(): void {
this.heroForm = this.fb.group({
'name': [this.hero.name, [
Validators.required,
Validators.minLength(4),
Validators.maxLength(24),
forbiddenNameValidator(/bob/i)
]
],
'alterEgo': [this.hero.alterEgo],
'power': [this.hero.power, Validators.required]
});
this.heroForm.valueChanges
.subscribe(data => this.onValueChanged(data));
this.onValueChanged(); // (re)set validation messages now
}
onValueChanged(data?: any) {
if (!this.heroForm) { return; }
const form = this.heroForm;
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
}
formErrors = {
'name': '',
'power': ''
};
validationMessages = {
'name': {
'required': 'Name is required.XXX',
'minlength': 'Name must be at least 4 characters long.',
'maxlength': 'Name cannot be more than 24 characters long.',
'forbiddenName': 'Someone named "Bob" cannot be a hero.'
},
'power': {
'required': 'Power is required.'
}
};
}
それは退屈です...誰かが理由なしでこの妥当な質問を閉じたいと思っています... – Pascal