简体   繁体   中英

How to assign keys and values dynamically

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>

You can assign keys and values dynamically like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM