I have a component with :
Example :
selectedData = { Label:"test", IsUsed:false}
fields = [{name:'label', type:'string'},{name:'IsUsed, type:'boolean'}
I am trying to generate a form for editing the data inside 'selectedData'
Here is the part of my view :
<div class="ui-grid-row" *ngFor="let myField of fields" >
<div class="ui-grid-col-4"><label for="{{myField.name}}">{{myField.name}}</label></div>
<div class="ui-grid-col-8"><input pInputText id="{{myField.name}}" [(ngModel)]="this['selectedData.' + myField.name]" /></div>
</div>
The problem is with this line
[(ngModel)]="this['selectedData.' + myField.name]"
It does not work as I wanted. Here is the Google Chrome watch of my component when i edit :
As you can see, Angular2 creates an object with name "selectedData.Label" instead of using the selectedData existing object.
Is there a solution? Or should I do it differently?
Thx
The answer was easy :
[(ngModel)]="selectedData[myField.name]"
If field.Name has a value of 'label', the previous code is equivalent as :
[(ngModel)]="selectedData.label"
I did not know that the '[ ]' could be used this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With