2017-11-17 2 views
0
// setup the form 
const formGroup = {}; 
for (const prop of Object.keys(this.dataObject)) { 
    try { 
    const cpv = this.dataObject[prop].cpv; 
    const value = this.dataObject[prop].value; 
    formGroup['componentDetails'] = new FormArray([ 
     new FormControl({'cpv': cpv, 'value': value}) 
    ]); 
    } catch (e) { 
    console.log('Exception in Form setup - ' + e); 
    } 
} 

Ich habe den oben genannten Code zum Einrichten von Formularsteuerung und Formular-Array für mein Formular. Das resultierende JSON, die erzeugt wird, nicht Wert Eigenschaft hinzufügen :(Angular Form Array von Objekt funktioniert nicht

Was ich bekommen.

"componentDetails": [ { "cpv": "CPV_1" } ]

Was ich brauche

"componentDetails": [ { "cpv": "CPV_1", "value": "test value" } ]

Ich sehe keine Fehler War frage mich, ob Sie jemand das gleiche Problem konfrontiert und wie sie es gelöst?

+0

Nun, Sie sind ein Formcontrol nur zu schaffen, wenn du offensichtlich zwei brauchst. Das ist zumindest ein Problem auf den ersten Blick :) – Alex

+0

cpv und Wert müssen zusammen gehen, wenn wir das Formular einreichen ... [{'cpv': 'cpv1', 'value': 'test1'}, {' cpv ':' cpv2 ',' Wert ':' test2 '}] –

+0

@DavidJeyathilak Dann könnten Sie sie als ein Objekt erstellen; Überprüfen Sie meine Bearbeitung – Milo

Antwort

1

Als @ AJT_82 Sugg sierte value müsste eine andere FormControl sein:

formGroup['componentDetails'] = new FormArray([ 
    new FormControl({'cpv': cpv}), 
    new FormControl({''value': value}) 
]); 

EDIT: Wenn sie benötigen, als ein Paar zu gehen:

const cpv = this.dataObject[prop].cpv; 
const value = this.dataObject[prop].value; 
let pair: any = { 'cpv': cpv, 'value': value }; 

formGroup['componentDetails'] = new FormArray([ 
    new FormControl(pair) 
]); 

See: https://angular.io/api/forms/FormArray

+0

Ich habe das versucht, aber nicht funktioniert :( –

Verwandte Themen