Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从FormBuilder传递对象时出现问题_Angular_Typescript_Angular Reactive Forms_Angular2 Formbuilder_Angular Formbuilder - Fatal编程技术网

Angular 从FormBuilder传递对象时出现问题

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

当我试图将对象从formBuilder传递到typescript文件的另一个AbstractControl函数时

但在传递数据时无法完成


下面是代码片段

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