2017-04-26 2 views
0

こんにちは私は、Angular2で事前に書式を設定し、ユーザーが事前入力データを変更することを許可しません。私はちょうどこのprefilledデータをサーバーに投稿したい(データは現在ログインしているユーザーの情報から自動的に得られる)。Angular2でプレフィルフォームを作成する方法は?

どうすればこの問題を解決できますか?

私のコード。

<form [formGroup]="requestForm" (ngSubmit)="onSubmit()"> 
    <div class="form-group"> 

<label class="fontstyle1"><span class="fontstyle2">STEP2. </span>User Information</label> 
    <div class="form-group"> 
     <label class="fontstyle3">Name</label> 
     <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" formControlName="name"[(ngModel)]="selected_name" value="user.name"> 
    </div> 

    <div class="form-group"> 
     <label class="fontstyle3">E-mail</label> 
     <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" formControlName="email" [(ngModel)]="selected_email" value="user.email"> 
    </div> 

     <div class="form-group"> 
     <label class="fontstyle3">Phone</label> 
     <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" formControlName="phone" [(ngModel)]="selected_phone" value="user.phone"> 
    </div> 

    <br> 

     <button type="submit" id="request-btn">Request</button> 

</div> 

私はこれを実行すると、結果は不変でもプレフィルドでもありません。

ヘルプ!

ありがとうございました!

+0

値 'へ= "{{user.phone}}"'値= "user.phone" 'から変更しようとを形成するためにデータを設定' –

+1

複数の入力と同じ' formControlName'を使用しないことをお勧めします。また、反応的なフォームとモデルの結合を一緒に使用することはお勧めしません。反応しやすいフォームで簡単に何でもできますので、[this](https://angular.io/docs/ts/latest/guide/reactive-forms.html)をご覧ください。 – ulubeyn

+0

前述のように、同じ 'formControlName'です。同じ 'formControlName'を使用する場合は、' FormArray'を使用する必要があります:) – Alex

答えて

1

formControlNameまたはngModelのいずれかを使用してください。また、入力フィールドも無効にしてください。 ngOnInitに

<form [formGroup]="requestForm" (ngSubmit)="onSubmit()"> 
    <div class="form-group">  
     <label class="fontstyle1"><span class="fontstyle2">STEP2. </span>User Information</label> 
     <div class="form-group"> 
      <label class="fontstyle3">Name</label> 
      <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" [(ngModel)]="user.name" disabled> 
     </div> 

     <div class="form-group"> 
      <label class="fontstyle3">E-mail</label> 
      <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" [(ngModel)]="user.email" disabled> 
     </div> 

     <div class="form-group"> 
      <label class="fontstyle3">Phone</label> 
      <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" [(ngModel)]="user.phone" disabled> 
     </div>  
     <br>  
     <button type="submit" id="request-btn">Request</button> 
     </div> 
    </div> 
</form> 

そしてコンポーネントに

ngOnInit(){ 
    this.apicall.subscribe((info)=>{ 
     this.currentUser = info; 
    } 
); 
} 
関連する問題