2017-04-27 151 views
0

我是新的angular2,我想要插入数据的表单我的问题是当我点击创建它显示“浏览器同步断开”也最初两个字段的名称和地址显示我给的值他们,但邮编doesn't.and终于有错误错误:“无法找到路径控制: 'ADRESS - > ADRESS' 这里是我的代码 component.htmlangular2表单验证错误消息

<div class="container"> 
<h3>Add user:</h3> 
<form [formGroup]="myForm" novalidate (ngSubmit)="Create(myForm.value, myForm.valid)"> 
<ul> 
    <div class="form-group"> 
     <label for="name">name:</label> <input type="text" class="form-control" formControlName="name"><br/><small [hidden]="myForm.controls.name.valid || (myForm.controls.name.pristine && !submitted)" class="text-danger"> 
     name is required. 
      </small> 
      </div> 

<div class="form-group" formGroupName="adress"> 
     <label for="">adress</label> 
     <input type="text" class="form-control" formControlName="street"> 
     <small [hidden]="myForm.controls.adress.controls.street.valid || (myForm.controls.adress.controls.street.pristine && !submitted)" class="text-danger"> 
      street required 
      </small> 


<div class="form-group" formGroupName="adress"> 
     <label for="">postcode</label> 
     <input type="text" class="form-control" formControlName="postcode"> 
    </div> 

<button class="btn btn-default" (click)="CreateVersion()">create</button> 
     </div> 

    </ul> 
    </form> 
    </div> 

component.ts

import { Component ,OnInit} from '@angular/core'; 
import {Service} from '../services/service.component'; 
import { FormsModule,FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms'; 
import {Observable} from 'rxjs/Rx'; 
import { User } from './user.interface'; 


@Component({ 
moduleId: module.id, 
selector: 'version', 
templateUrl:'./version.component.html', 
styleUrls:['./version.component.css'] 
}) 

export class VersionComponent implements OnInit{ 

public myForm: FormGroup; 


constructor(){}; 

    ngOnInit() { 

     this.myForm = new FormGroup({ 
      name: new FormControl('', [<any>Validators.required, <any>Validators.minLength(5)]), 
      adress: new FormGroup({ 
       street: new FormControl('', <any>Validators.required), 
       postcode  : new FormControl('') 
      }) 
     }); 

    const people = { 
       name: 'Jae', 
       adress: { 
       street: 'High street', 
       postcode: '94043' 
      } 
     }; 

     (<FormGroup>this.myForm) 
      .setValue(people, { onlySelf: true }); 
} 
    Create(conf: User, isValid: boolean) { 
     this.submitted = true; 
     console.log(model, isValid); 
    } 
} 

用户

export interface User { 
name: string; 
    adress?: { 
    street?: string; 
    postcode?: string; 
    } 
    } 
+0

它在哪里看到的页面 –

+0

了“断开浏览器同步”左侧,它重新加载页面 – sarra

+0

这只是代码的一部分,我看不到页面重新加载。 –

回答

2

我想这是因为你已经有formGroupName包裹你的邮编两次,你的HTML应该是这样的: -

<div class="container"> 
    <h3>Add user:</h3> 
    <form [formGroup]="myForm" novalidate (ngSubmit)="Create(myForm.value, myForm.valid)"> 
     <ul> 
      <div class="form-group"> 
       <label for="name">name:</label> <input type="text" class="form-control" formControlName="name"><br/> 
       <small [hidden]="myForm.controls.name.valid || (myForm.controls.name.pristine && !submitted)" 
         class="text-danger"> 
        name is required. 
       </small> 
      </div> 

      <div class="form-group" formGroupName="adress"> 
       <label for="">adress</label> 
       <input type="text" class="form-control" formControlName="street"> 
       <small [hidden]="myForm.controls.adress.controls.street.valid || (myForm.controls.adress.controls.street.pristine && !submitted)" 
         class="text-danger"> 
        street required 
       </small> 

       <label for="">postcode</label> 
       <input type="text" class="form-control" formControlName="postcode"> 

       <button class="btn btn-default" (click)="CreateVersion()">create</button> 
      </div> 

     </ul> 
    </form> 
</div> 
+0

好,所以邮编现在显示,但它是相同的,当我点击创建它重新加载页面和“浏览器同步断开连接”显示 – sarra

+0

太棒了,你是如何运行网站 - 与吞咽或精简版服务器或类似的东西? –

+0

我正在使用快递服务器 – sarra