使用angular2指定ionic2中文本框的最小长度

时间:2018-01-10 08:54:23

标签: angular typescript ionic-framework

我正在验证带有angular2的文本框,它可以正常工作。现在我正在设置要在文本框中输入的最小字符集,但是当我尝试时出现此错误:

Expected validator to return a Promise or Observable

以下是我的剧本

customerNumber: any;

  constructor(public navCtrl: NavController,
              public viewctrl: ViewController,
              public formBuilder: FormBuilder,
              public http: Http,
              public alertCtrl: AlertController,
              public loadingCtrl: LoadingController,
              public toastCtrl: ToastController) {

                this.verifyForm = this.formBuilder.group({
                  customerNumber: ['', Validators.required,Validators.minLength(5)]
                });
  }

HTML

<ion-item>
 <ion-input type="text" formControlName="customerNumber" placeholder="Enter your Customer Number"  [(ngModel)]="customerNumber"></ion-input>
</ion-item>

2 个答案:

答案 0 :(得分:1)

customerNumber: any;

  constructor(public navCtrl: NavController,
              public viewctrl: ViewController,
              public formBuilder: FormBuilder,
              public http: Http,
              public alertCtrl: AlertController,
              public loadingCtrl: LoadingController,
              public toastCtrl: ToastController) {

                this.verifyForm = this.formBuilder.group({
                  customerNumber: ['', Validators.compose([Validators.required, Validators.minLength(5)])]
                });
  }

答案 1 :(得分:1)

您将Validators.minLength(5)作为第三个参数传递,这是一个异步验证器

应该是

this.verifyForm = this.formBuilder.group({
                  customerNumber: ['', [Validators.required,Validators.minLength(5)]]
                });