2017-09-20 36 views
1

angularjs FormGroup有效的是事實,但返回falseangularjs FormGroup有效的是事實,但返回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

+0

這意味着在FormChange方法被調用後,表單的狀態已經改變。 'console.log'顯示對象的當前狀態,而不是調用時的快照對象。 – yurzui

回答

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

相關問題