Validation Angular 2无法读取属性';订阅';使用Validators.pattern时表单字段的空值

Validation Angular 2无法读取属性';订阅';使用Validators.pattern时表单字段的空值,validation,typescript,angular2-forms,Validation,Typescript,Angular2 Forms,我正在研究角度2反应形式。我在使用Validators.pattern()进行表单字段验证时遇到了一个问题。一旦我开始在输入字段中输入,就会出现异常无法读取null的属性“subscribe” TYPSCRIPT代码 createForm(){ this.jobForm = this.fb.group({ name: ['', Validators.required, Validators.pattern(/^[a-zA-Z0-9]+$/)], epTyp

我正在研究角度2反应形式。我在使用Validators.pattern()进行表单字段验证时遇到了一个问题。一旦我开始在输入字段中输入,就会出现异常
无法读取null的属性“subscribe”

TYPSCRIPT代码

createForm(){
    this.jobForm = this.fb.group({
        name: ['', Validators.required, Validators.pattern(/^[a-zA-Z0-9]+$/)],
        epType: ['ENDPOINT_TYPE_SBC', Validators.required],
        streams : this.fb.array([
            this.createStreamControls(),
        ]),
        channels:this.fb.array([
            this.createChannelControls(),
        ])
    });        
}
 <div class="ui-grid-col-8 ">
      <input id="name" formControlName="name" pInputText class="width-60" />
                    <br>
      <small *ngIf="jobForm.controls.name.hasError('required') && jobForm.controls.name.touched" class="text-danger">Required</small>
      <small *ngIf="jobForm.controls.name.hasError('pattern') && jobForm.controls.name.touched" class="text-danger">Should be alpha-numeric without space</small>
 </div>
根据模式验证“名称”中的属性

HTML代码

createForm(){
    this.jobForm = this.fb.group({
        name: ['', Validators.required, Validators.pattern(/^[a-zA-Z0-9]+$/)],
        epType: ['ENDPOINT_TYPE_SBC', Validators.required],
        streams : this.fb.array([
            this.createStreamControls(),
        ]),
        channels:this.fb.array([
            this.createChannelControls(),
        ])
    });        
}
 <div class="ui-grid-col-8 ">
      <input id="name" formControlName="name" pInputText class="width-60" />
                    <br>
      <small *ngIf="jobForm.controls.name.hasError('required') && jobForm.controls.name.touched" class="text-danger">Required</small>
      <small *ngIf="jobForm.controls.name.hasError('pattern') && jobForm.controls.name.touched" class="text-danger">Should be alpha-numeric without space</small>
 </div>


要求的 应该是字母数字,没有空格
正在尝试验证输入字段名

异常
无法读取null的属性“subscribe”


当我从name属性中删除Validators.pattern()时,它可以正常工作。我无法理解为什么它在这里失败,因为我对其他属性使用了相同表单中的Validators.pattern(),并且对它们很好。

需要在表单组的第二个参数中传递验证器数组

createForm(){
    this.jobForm = this.fb.group({
        name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9]+$/)]],
        epType: ['ENDPOINT_TYPE_SBC', [Validators.required]],
        streams : this.fb.array([
            this.createStreamControls(),
        ]),
        channels:this.fb.array([
            this.createChannelControls(),
        ])
    });        
}

哦,是的,我的错。我被这个问题困扰了两天。你犯了多小的错误啊(感谢您的帮助:)我想应该是
['',Validators.compose([Validators.required,Validators.pattern(/^[a-zA-Z0-9]+$/)])]
明白了@swaroop强调了我所犯的错误。谢谢你的帮助