Angular2를 배우기 위해 인보이스 발행 앱을 구축 중입니다. 내가 부딪히는 문제는 행에 3 개의 입력이 포함되어 있고 행 항목의 배열에있는 객체에 바인딩해야하는 행 항목 구성 요소를 작성하는 방법입니다.오브젝트 입력 배열이있는 각도 2 형식
1 각도에서는 입력의 컨테이너에 ng-form
지시어를 추가하여 쉽게 얻을 수 있습니다. 여기에 상응하는 것은 무엇입니까?
HTML :
<form name="form" ng-submit="$ctrl.submit(form)" novalidate>
<!-- some more code -->
<ul class="list invoice-table__body">
<li *ngFor="let item of model.lineItems; let i = index">
<input
type="text"
name="description"
class="col-sm-8"
[(ngModel)]="item.description">
<input
type="number"
name="quantity"
class="col-sm-2"
[(ngModel)]="item.quantity">
<input
type="number"
name="total"
class="col-sm-2"
[(ngModel)]="item.total">
</li>
</ul>
<!-- some more code -->
</form>
구성 요소 :
import { Component } from '@angular/core';
import { Invoice } from './invoice.model';
import { InvoiceLineItems } from './invoice-line-item.model';
@Component({
selector: 'create-invoice',
templateUrl: 'create-invoice/create-invoice.html'
})
export class CreateInvoiceComponent {
model: Invoice = new Invoice(
85,
'CAD',
null,
[ // lineItems
new InvoiceLineItems('Web development for BAnQ'),
new InvoiceLineItems('Sept 1 to 3', 14, 910),
new InvoiceLineItems('Sept 5 to 10', 34, 5293),
new InvoiceLineItems('Sept 11 to 20', 24, 5293),
new InvoiceLineItems('Sept 21 to 38', 11, 2493),
],
13989,
100,
200,
15000,
'',
null,
'$'
);
getTotal(): number {
return this.model.lineItems.reduce(
(a, b): number => a + (isNaN(b.total) ? 0 : b.total),
0);
}
}
어떤 문제가 있습니까? 전혀 표시되지 않거나 바인딩이 표시되지 않습니까? 빌드 오류 또는 콘솔 오류? – NPhillips
마지막 배열 항목의 값이 5 번 표시됩니다. 오류 없음. 이상한 점은 입력 값을 변경하면 배열의 올바른 객체가 변경된다는 것입니다. – justinledouxweb
입력 된 이름과 인덱스를 연결하면 잘 작동하지만 각도의 2 방법이 아닌지 확실하지 않습니다 ... – justinledouxweb