Angular 从FormBuilder传递对象时出现问题
当我试图将对象从formBuilder传递到typescript文件的另一个AbstractControl函数时 但在传递数据时无法完成Angular 从FormBuilder传递对象时出现问题,angular,typescript,angular-reactive-forms,angular2-formbuilder,angular-formbuilder,Angular,Typescript,Angular Reactive Forms,Angular2 Formbuilder,Angular Formbuilder,当我试图将对象从formBuilder传递到typescript文件的另一个AbstractControl函数时 但在传递数据时无法完成 下面是代码片段 Main.component.ts import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms'; import { PasswordStrengthValidator } from './PasswordStrengthValidator';
下面是代码片段 Main.component.ts
import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms';
import { PasswordStrengthValidator } from './PasswordStrengthValidator';
let obj = {};
obj.passwordLength = 12;
obj.lowercaseNo = 3;
...
this.passwordForm = this.formBuilder.group({
password: new FormControl('', Validators.compose([
Validators.required,
Validators.minLength(obj.passwordLength),
Validators.maxLength(30),
PasswordStrengthValidator
])),
}, {
validators: this.password.bind(this)
});
}
PasswordStrengthValidator.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';
export const PasswordStrengthValidator = function (control: AbstractControl): ValidationErrors | null {
console.log(obj); // object values needs to be displayed here
...
}
是否可以将上述代码替换为主要组件本身,或者是否有更好的方法来解决此问题您可以使用工厂:
这个问题不是很清楚。你能试着用一个明确的目标来设置一个最小的可重复性问题吗?@Guilhermevrs请参考
PasswordStrengthValidator.ts
和app.component.ts
filegreat!。。。我已经调用了this.myForms=fb.group({…
在另一个函数中,因为它将用空数组覆盖初始值。通过使用wait
&async
在获得任何响应后是否可以调用此方法/通过其他方法结束(例如,此方法在序列中调用的另一个方法中调用this.myForms..
之前初始化参数值)通过wait
和async
。。。
export const PasswordStrengthValidatorFactory = (passObject: any) => {
return function(control: AbstractControl): ValidationErrors | null {
// In console required to display those values in the passObject
console.log(passObject);
return null;
};
};