2017-06-12 85 views
1

这里IM Implementin简单AngularForm我在构成元素一流的进口这CLAAngular2反应形式验证错误control.registerOnChange不使用Angular2</p> <pre><code>export class Customer { customerName: String = "";} </code></pre> <p>功能

import { Component, OnInit } from "@angular/core" 
import { NgForm, FormGroup, FormControl, Validators, FormBuilder } from "@angular/forms" 
import { Customer } from "../template/customer" 

@Component({ 
    selector: "ui-view", 
    templateUrl: "../template/customer.html" 
}) 

    export class CustomerComponent implements OnInit { 
     currentCustomer: Customer = null; 
     customerForm: FormGroup; 
     custobj: Customer = new Customer(); 
     ngOnInit(): void { 
      this.customerForm = new FormGroup({ 
       customerName: new FormGroup({ 

       }) 
      }) 
     } 

我的HTML代码即时得到Errror作为添加formControlName是东西...即时通讯混淆我在哪里做错了,请帮助我

<form [formGroup]="customerForm"> 
    <div class="form-group"> 
     <label for="name">Name</label> 
     <input type="text" class="form-control" [formControlName]="customerName" /> 
    </div> 
<button type="submit" class="btn btn-success btn-block" [disabled]="!customerForms.valid">Submit</button> 
</form> 

回答

0

一个问题,我可以马上看到的是,您使用的属性绑定时调用[formControlName],这意味着你的语法应该是这样的

[formControlName]="'customerName'" 

但最好只使用

formControlName = "customerName" 

此外,由于您正在为此输入它不应该是一个新的FormGroup,而是customerName: new FormControl()

相关问题