How to assign keys and values dynamically

时间:2017-06-19 13:58:12

标签: javascript angular angular2-template angular2-directives

I want to collect user input details as key value pair.Suppose,in my view side two input fields and added as key value pair in the object.Using onclick events more set of input fields are generated dynamically.So then the key value pair added dynamically in the object

I try to implement it in angular2 using input event but no success.

currencyObj: any = {
partnerId: '',
currency: '',
val: '',`enter code here`
interactionId: '',
details: {}  
};

in template:

<div class="col-md-6">
      <div class="group">
         <button id="buttonlogintoregister" class="small pull-right" type='button' (click)="counterAdd()">Add Fields <span class="glyphicon glyphicon-plus-sign"></span></button>
      </div>
    </div>
  <hr>
  <div *ngFor='let count of counter'>
 <div class="col-md-12">
    <div class="col-md-6">
      <div class="group">
        <input class="inputMaterial" type="text"  [(ngModel)]="currencyObj.details[input1]" required>
        <span class="highlight"></span>
        <span class="bar"></span>
        <label>Field1<span style="color:red;"> *</span></label>
      </div>
    </div>
    <div class="col-md-6">
      <div class="group">
        <input class="inputMaterial" type="text" [(ngModel)]="currencyObj.details[input1]=input2" required>
        <span class="highlight"></span>
        <span class="bar"></span>
        <label>Field2<span style="color:red;"> *</span></label>
      </div>
    </div>
  </div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

You can assign keys and values dynamically like this:

var k = 'key';
currencyObj[k] = 'value';