2016-04-11 17 views
1

テーブル内の任意のプレーヤーの番号を送信したいと考えています。ngFor生成テーブルのフォーム入力値

私はAngular2の* ngForでテーブルを生成しています。
そのテーブルの各要素に対して、入力フィールドを含むフォームを追加します。

これらのフォームの入力値を送信して含めるにはどうすればよいですか?

<table> 
    <tr> 
     <th>Name</th> 
     <th>Value</th> 
     <th>Bid</th> 
    </tr> 
    <tr *ngFor="#player of players"> 
     <td>{{player.name}}</td> 
     <td>{{player.value | currency:'GBP':true:'4.0-0'}}</td> 
     <td> 
      <form role="form" (submit)="onBid($event, player)"> 
       <input type="number" min={{player.value}} value={{player.value}}> 
       <button type="submit">Bid</button> 
      </form> 
     </td> 
    </tr> 
</table> 

入力ボックスから値を送信して取得できませんでした。
id="inputname"#inputnameを定義し、次に(submit)="onBid(inputname.value)"の作品にinputname.valueを追加する静的形式の場合は、

私はadding id={{player.id}}#{{player.id}}を試しましたが、それをonBid()に追加する方法はわかりません。

+0

あなたは "を提出して検索する" とはどういう意味ですか? '(submit)=" onBid(inputname.value) "'の問題は何ですか? –

答えて

2

working Demo

<td> 
      <form role="form" (submit)="onBid($event, player, name.value)"> 
       <input type="number" #name min={{player.value}} value={{player.value}}> 
       <button type="submit">Bid</button> 
      </form> 
</td> 

onBid(e,player,value) 
{ 
     player.inputValue=value; //<-----this will add new property to your existing object with input value. 
     console.log(player); 
} 
+0

ありがとう、**#名前**それを解決!私はそれがユニークでなければならず、それを忘れてしまったと思ったので、私は各フォームの固有の名前を生成することに集中しました。 – larskris

+0

ようこそ@ user1134573.NP! – micronyks

1

フォーム全体を「投稿」したい場合は、ngModelを配列またはオブジェクトにバインドするのはなぜですか?

@Component({ 
    selector: 'my-app', 
    template: ` 
    <form role="form" (submit)="onBid($event, player)"> 
    <table> 
    <tr> 
     <th>Name</th> 
     <th>Value</th> 
     <th>Bid</th> 
    </tr> 
    <tr *ngFor="#player of players; #i=index"> 
     <td>{{player.name}}</td> 
     <td>{{player.value | currency:'GBP':true:'4.0-0'}}</td> 
     <td> 
     <input type="number" [(ngModel)]="bids[i]" 
       min="{{player.value}}" value="{{player.value}}"> 
     </td> 
    </tr> 
    </table> 
    <button type="submit">Bid</button> 
    </form> 
    ` 
}) 
export class AppComponent { 
    constructor() { 
    this.players = [ 
     (...) 
    ]; 
    this.bids = []; 
    } 

    onBid() { 
    console.log(this.bids); 
    } 
} 

と物体との:ここ

アレイを有するサンプルであるhttps://plnkr.co/edit/Ox4HmliuX3ESdf8JIZgr?p=preview

@Component({ 
    selector: 'my-app', 
    template: ` 
    <form role="form" (submit)="onBid($event, player)"> 
    <table> 
    <tr> 
     <th>Name</th> 
     <th>Value</th> 
     <th>Bid</th> 
    </tr> 
    <tr *ngFor="#player of players; #i=index"> 
     <td>{{player.name}}</td> 
     <td>{{player.value | currency:'GBP':true:'4.0-0'}}</td> 
     <td> 
     <input type="number" [(ngModel)]="bids[player.name]" 
       min="{{player.value}}" value="{{player.value}}"> 
     </td> 
    </tr> 
    </table> 
    <button type="submit">Bid</button> 
    </form> 
    ` 
}) 
export class AppComponent { 
    constructor() { 
    this.players = [ 
     (...) 
    ]; 
    this.bids = {}; 
    } 

    onBid() { 
    console.log(this.bids); 
    } 
} 

このplunkrを参照してください。

関連する問題