Angular 5表单验证,不访问验证器属性

时间:2018-01-29 11:54:18

标签: twitter-bootstrap forms validation angular5

我创建了一个注册表单并尝试使用Angular 5表单的验证器进行验证。 我已经通过StackOverflow上的其他线程尝试了解决方案 - Angular 5 ngx-bootstrap form validation 但我无法解决我的问题。

我的HTML是 -

<div class="was-validated">
  <form [formGroup]="myform" novalidate role="form">
    <div class="row main">
      <div class="main-login main-center" style="padding-top:0px;">
        <form class="form-horizontal" method="post" action="#">
          <div class="row">
            <div class="col-md-5">
              <div class="form-group"
                [ngClass]="{
                'has-success': firstname.valid && (firstname.dirty || firstname.touched),
                'has-danger': firstname.invalid && (firstname.dirty || firstname.touched)
                 }">
                 <label for="firstname" class="cols-sm-2 control-label">First Name</label>
                 <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                 <input formControlName="firstname" type="text" class="form-control" name="firstname" id="firstname" placeholder="Enter your First Name" required/>
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
  </form>
</div>

我的组件 -

    import { Component, OnInit, Pipe, NgModule } from '@angular/core';
    import { Http } from '@angular/http/src/http';
    import { resetFakeAsyncZone } from '@angular/core/testing';
    import{HttpClient, HttpParams, HttpHeaders} from'@angular/common/http';
    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/observable/forkjoin';
    import { forkJoin } from 'rxjs/observable/forkJoin';
    import {
             ReactiveFormsModule,FormsModule,FormGroup,FormControl,Validators,FormBuilder
     } from '@angular/forms';
    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']  
    })
    export class AppComponent implements OnInit {
    myform: FormGroup;
    firstname: FormControl;
    constructor (private http:HttpClient){ }
    ngOnInit(){
    this.createFormControls();
    this.createForm();
    }
    createFormControls(){
    this.firstname= new FormControl('', [Validators.required,
    Validators.pattern("/^[A-Za-z]+$/"), Validators.maxLength(20)])
    }
    createForm(){
    this.myform = new FormGroup({
    firstname: this.firstname
    });
    }
    }

感谢。

0 个答案:

没有答案