2017-08-31 13 views
1

私はAngular2に新しいですが...私の問題は十分に簡単です:角度2モデルバインディング(オブジェクトの配列)

私は次のコンポーネント(アプリを持っている:私は次のようにオブジェクトの配列をバインドします。 TS)及び図(cart.html):

(app.ts)ファイル:

@Component({ 
selector: 'my-app', 
templateUrl: '../Partials/cart.html', 
styleUrls: ['../Partials/cart.css'] 
}) export class AppComponent{ 

constructor(private http: Http) { }; 
title = 'ASP.NET MVC 5 with Angular 2'; 
private headers = new Headers({ 'Content-Type': 'application/json' }); 

invoice: Invoice = {  

    customer: { 
     CustomerName: "Lorem Ipsum", 
     Address: "102/102 East Hills Road", 
     Suburb: "East Hills", 
     State: "NT", 
     PostCode: "3563"    
    }, 
    products: [ 
     { 
      id: "1", 
      desc: "Mig Mac", 
      unit_price: 8.5, 
      quantity: 5 
     }, 
     { 
      id: "2", 
      desc: "Fillet O Fish", 
      unit_price: 4.50, 
      quantity: 3 
     }, 
     { 
      id: "3", 
      desc: "Icecreme", 
      unit_price: 0.5, 
      quantity: 10 
     } 

    ] 

} 

invoice2: Invoice; 


getTotal = function() { 

    var total = 0; 
    for (var i = 0; i < this.invoice.products.length; i++) 
    { 
     total += this.invoice.products[i].unit_price * this.invoice.products[i].quantity; 
    } 
    return total; 
} 


onSubmit() { 

    this.create().then(i => { 
     this.invoice2 = i; 
     console.log(this.invoice2); 
    }); 
} 
create(): Promise<Invoice> { 
    console.log("POST"); 
    let url = "/Invoice/Create"; 
    return this.http.post(url, this.invoice).toPromise() 
     .then(res => { return res.json() as Invoice }); 

} 

}; 

export class Invoice { 
customer: Customer; 
products: Item[]; 
} 

export class Customer { 
CustomerName: String; 
Address: String; 
Suburb: String; 
State: String; 
PostCode: String; 

} 

export class Item { 
id: String; 
desc: String; 
unit_price: Number; 
quantity: Number; 
} 

(cart.html)ファイル:

<form #f="ngForm" (ngSubmit)="onSubmit()"> 

<div class="invoice-wrap"> 

    <div class="invoice-header"> 

     <div class="container-fluid"> 

      <div class="row"> 

       <div class="col-xs-6 col-xxs-12"> 
        <div class="customer-details"> 

         <p><strong>Invoice To</strong></p> 
         <input type="text" [(ngModel)]="invoice.customer.CustomerName" name="CustomerName" /> 

         <span><strong>Address: </strong></span><br /> 

         <input type="text" [(ngModel)]="invoice.customer.Address" name="Address" /><br /> 
         <input type="text" [(ngModel)]="invoice.customer.Suburb" name="Suburb" /><br /> 
         <input type="text" [(ngModel)]="invoice.customer.State" name="State" /><br /> 
         <input type="text" [(ngModel)]="invoice.customer.PostCode" name="PostCode" /><br />       

        </div> 


       </div> 
       <div class="col-xs-6 col-xxs-12"> 
        <button type="submit" [disabled]="!f.valid">Submit</button> 
       </div> 

       <div class="clearfix"></div> 
      </div> 
     </div> 

    </div> 
</div> 



<div class="item-header-wrap"> 

    <div class="item-header hidden-xs"> 

     <div class="container-fluid"> 

      <div class="row"> 

       <div class="col-sm-2 col-xs-4"> 

        <p>Product</p> 

       </div> 
       <div class="col-sm-3 col-xs-8"> 
        <p>Description</p> 

       </div> 

       <div class="col-sm-2 col-xs-12"> 
        <p>Delivery</p> 

       </div> 

       <div class="col-sm-2 col-xs-12"> 
        <p>Click&amp;Collect</p> 


       </div> 

       <div class="col-sm-1 col-xs-6"> 
        <p>Unit Price</p> 


       </div> 

       <div class="col-sm-1 col-xs-6"> 
        <p>Quantity</p> 

       </div> 

       <div class="col-sm-1 col-xs-12"> 
        <p>Line Total</p> 

       </div> 



      </div> 
     </div> 
    </div> 
</div> 





<div class="item-wrap"> 

    <div class="item" *ngFor="let product of invoice.products"> 

     <div class="container-fluid"> 

      <div class="row"> 

       <div class="col-sm-2 col-xs-4"> 
        <div class="azure col"> 


         <img src="./images/item.jpg" class="item-img" /> 

        </div> 

       </div> 
       <div class="col-sm-3 col-xs-8"> 
        <div class="orange col"> 

         <input type="text" [(ngModel)]="product.desc" name="product.desc" value="" /> 


        </div> 

       </div> 

       <div class="col-sm-2 col-xs-12"> 
        <div class="azure col"> 

        </div> 

       </div> 

       <div class="col-sm-2 col-xs-12"> 
        <div class="click-and-collect col"> 


        </div> 


       </div> 

       <div class="col-sm-1 col-xs-6"> 
        <div class="red col"> 

         <input type="text" [(ngModel)]="product.unit_price" name="product.unit_price" value="" /> 

        </div> 


       </div> 

       <div class="col-sm-1 col-xs-6"> 
        <div class="col"> 
         <input type="text" [(ngModel)]="product.quantity" name="product.quantity" value="" /> 

        </div> 


       </div> 

       <div class="col-sm-1 col-xs-12"> 
        <div class="red col"> 
         <p>${{product.unit_price * product.quantity}}</p> 
        </div> 


       </div> 



      </div> 
     </div> 
    </div> 






</div> 

<div class="total"> 
    Total = {{getTotal()}} 
</div> 

ブラウザで次のように表示されます。ここでは、モデルにオブジェクト配列の最後の要素が設定されています。 enter image description here お客様請求書オブジェクトの一部が正しくバインドされていますが、オブジェクトの製品配列が正しくバインドされていません。私は何を間違っているのですか?私はGoogleで広範に検索し、さまざまな方法を試しましたが、何も動作していないようです。助けてください。

+0

入力フィールドからvalue = ""を削除して一度だけ試してください –

+0

value = ""が役に立たなかった。 – Choudhury

答えて

1

ngForループ内で複数のngModelコントロールを作成する場合は、それぞれが固有の名前を制御与えることを確認してください:あなたはこの

<div class="item" *ngFor="let product of invoice.products;let i=index"> 

と各入力フィールドのようにコードを変更することができます

はあなたが

として変更する必要があります

希望します。

+0

ありがとうございました。魅力的なように働いています... – Choudhury

+0

お待ちしています。 –

0

あなたが必要です電子ngForここでは、

<div class="item" *ngFor="let product of invoice.products;let i=index"> 
    <input type="text" name="name-{{i}}" [(ngModel)]="product.invoice.customer.Address"> 
0

あなたの問題はここにある:

​​

あなたのHTMLコードを使用すると、要素ごとに一意名前(入力)を使用する必要があり、フォーム内にあるので。しかし、あなたはおそらく、文字列の補間を使用することを忘れてしまったので、次の取得:あなたの問題を解決します名前の文字列補間を使用してproduct.desc

<input type="text" [(ngModel)]="product.desc" name="{{product.desc}}" value="" /> 

Actual DOM reflect すべての要素は、単に同じフォーム属性を参照してください。そして、あなたはngModelを使うどこでもこれを行うべきです。 また、同じ名前の複数のアイテムを含む請求書との重複を避けるために、名前に一意の識別子を追加する方が良い(「 - {{i}}」というインデックスを使用するなど)。

+0

2つの製品に同じ{{product.desc}}がある場合はどうなりますか?再び紛争が起きないだろうか? –

+0

はい、私は既に統一をサポートするために私の答えに編集を追加しました。これは、望ましいユーザ実装に依存する。私は自分自身で、@ Sajeetharanが示唆しているように、インデックスなどの一意の識別子を実際に使用するよう勧めます。これは、{{product.desc}} {{i}}になり、完全にユニークになります。 – ronenmiller

関連する問題