为自定义验证提供额外参数
Give extra parameter to custom validation
我正在试验 Angular2 表单验证并检查该值是否已被采用:
namesArray = Users[];
ngOnInit() {
this.myForm = this.fb.group({
name: ['', Validators.compose([
Validators.required,
this.nameValidator
])
],
})
}
nameValidator(control:FormControl):{[key:string]:boolean} {
console.log(this.namesArray);
return null;
}
这returns我一个错误:
无法读取未定义的 属性'namesArray'
当我打印 this
时 returns 未定义。那么如何访问函数之外的数组呢?
需要传入胖箭头函数才能保留this
ngOnInit() {
this.myForm = this.fb.group({
name: ['', Validators.compose([
Validators.required,
(control) => this.nameValidator(control as FormControl)
])
],
})
}
更多关于胖箭头的信息:https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html
我正在试验 Angular2 表单验证并检查该值是否已被采用:
namesArray = Users[];
ngOnInit() {
this.myForm = this.fb.group({
name: ['', Validators.compose([
Validators.required,
this.nameValidator
])
],
})
}
nameValidator(control:FormControl):{[key:string]:boolean} {
console.log(this.namesArray);
return null;
}
这returns我一个错误:
无法读取未定义的 属性'namesArray'
当我打印 this
时 returns 未定义。那么如何访问函数之外的数组呢?
需要传入胖箭头函数才能保留this
ngOnInit() {
this.myForm = this.fb.group({
name: ['', Validators.compose([
Validators.required,
(control) => this.nameValidator(control as FormControl)
])
],
})
}
更多关于胖箭头的信息:https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html