Angular 2 - dostęp do właściwości obiektu

0

Mam zdefiniowane klasy Invoice oraz CustomerShort jak poniżej. Chciałbym w formularzu móc edytować CustomerCode. Postanowiłem więc zrobić child FormGroup i odnieść się do tej właściwości. W następujący sposób:

<input [(ngModel)]="invoice.customer.customerCode" formControlName="customerCode" type="text" class="form-control wide inupt-bold" [readonly]="!isCustomerEditable">

Cały czas dostaję jednak komunikat, że customerCode jest niezdefiniowany. W jaki sposób odnieść się do tej właściwości? Sprawdzałem za pomocą console.log i przechowuje ona właściwą wartość.

    createForm() {
        this.invoiceForm = this._fb.group({
            customer: this._fb.group({
                customerCode: ['', Validators.required],
                name: ['', Validators.required],
            }),
            description: [],
            invoiceDate: ['', Validators.required],
            isExported: ['', Validators.required],
            invoiceDelivery: ['', Validators.required],
            priceCalculation: ['', Validators.required],
            paymentMethod: ['', Validators.required],
            paymentPeriod: []
        });
    }
export class Invoice {
    id: number;
    description: string;
    invoiceDate: string;
    isExported: boolean;
    invoiceDelivery: number;
    priceCalculation: number;
    paymentMethod: number;
    paymentPeriod: number;
    customerId: number;
    customer: CustomerShort;
    serviceItemSetId: number;

    invoiceItems: InvoiceItem[];
}

export class CustomerShort {
    id: number;
    customerCode: string;
    shippingCustomerCode: string;
    name: string;
    brandName: string;
    location: string;
    notes: string;
}


0

Napisz dokładnie jaki efekt chcesz uzyskac. Obczaj sobie interfejsy: doc. A jeśli chodzi o wyciaganie wartości z formularzy to idzie to mniej więcej tak na formGroupie wyszukujesz kontrolkę a następnie masz 2 możliwości .value oraz .getRawValue(), to drugie wyciąga także wartości z zablokowanych kontrolek. W twoim przypadku to będzie coś w stylu:

this.invoiceForm.get('customer').get('customerCode').value

1 użytkowników online, w tym zalogowanych: 0, gości: 1