Angular2 get values of dynamically created inputs
Asked Answered
A

2

6

I have this input which are created dynamically from list column, now I need to get all values of the inputs when some method occurs( imagine getAllValues() )

      <div *ngFor="let cell of column; let i = index;">
              <!-- Material design input-->
              <md-input type="{{cell.type}}" 
                 value="{{getInputValue(cell)}}" 
                 [placeholder]="cell.label">
              </md-input>
      </div>

Which would be the angular2 way of getting the values of all the generated inputs?

Acrolith answered 10/11, 2016 at 17:46 Comment(6)
Possible to use ViewChildren, if it's part of a form they can be appended to the form programmatically (reactive) or by magic (template driven).Teucer
@Teucer what would be the best practice? reactuve or magic?Acrolith
Template driven is more or less in line with the Angular 1 way of doing things, Reactive is heavier code wise but makes unit testing easier. Neither is best practice, template driven is probably a lower overhead approach in terms of learning and actual lines of code. If it's outside of a form then you'll probably want to do a ViewChildren query to grab them all.Teucer
@siletsod yes they are outside of form, how to grab all values with the ViewChildren is it possible to get value based on let i = index?Acrolith
If I get time I'll see about ginning up a demo plunker. In the meantime, hopefully the comments will help other prospective responders to produce a solution fitted for your use.Teucer
The angular-material tag should be removed since this doesn't relate to that library for Angular 1.x.Cooperate
C
8

The easiest way to do it is by using ngForm

<form #myForm="ngForm">
      <div *ngFor="let cell of column; let i = index;">
          <md-input [type]="cell.type" 
             [name]="cell.name"      <!-- Note the 'name' has to be set -->
             [ngModel]="cell.value"
             [placeholder]="cell.label"></md-input>
      </div>
      <a (click)="getAllValues(myForm)">print values</a>
</form>

Then you will have the access to myForm.form.value object in getAllValues() function. Plnkr: https://plnkr.co/edit/84mzcNJliMmvszPq3xMm?p=preview

Corrody answered 10/11, 2016 at 19:19 Comment(0)
A
0

What I did is:

              <md-input #input  // NOTICE #input
                          type="{{cell.type}}"
                          value="{{getInputValue(cell) || '--'}}"
                          [placeholder]="cell.label"></md-input>

in component class:

export class MyComponent {

    @ViewChildren('input') inputs;


    public updateData(): void {
        console.log(this.inputs) //this will give access to id and values (explore it on google chromes console)
        console.log(this.inputs.toArray().map(x => x.value)) // this gives access to values
    }
}
Acrolith answered 10/11, 2016 at 19:51 Comment(1)
Template variables can't be used this way when inputs are generated dynamically such as with an ngFor statementCheapjack

© 2022 - 2024 — McMap. All rights reserved.