angularjs FormGroup有效,但返回false

时间:2017-09-20 03:25:42

标签: javascript angular

angularjs FormGroup valid为true但返回false



 this.loginForm = new FormGroup({
      'phone': new FormControl(this.loginer.phone, [
        Validators.required,
        validateMobile
      ]),
      'password': new FormControl(this.loginer.password,[
        Validators.required
      ]),
  })
  public formChange(){
    console.log(this.loginForm);
    console.log(this.loginForm.valid);
  }




enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export class AppComponent implements OnInit {
    loginForm: FormGroup;
    constructor(private formBuilder: FormBuilder) {
        this.loginForm = this.formBuilder.group({
            phone: [null, Validators.compose([Validators.required])],
            password: [null, Validators.compose([Validators.required])]
        })
    }

    ngOnInit() {
        console.log(this.loginForm);
        console.log(this.loginForm.valid);
    }
}

Valid empty method