Angular2获取动态创建的输入的值

Com*_*ode 4 angular-material angular2-directives angular2-template angular2-material angular

我有这个从列表动态创建的输入column,现在我需要在某些方法发生时获取所有输入值(想象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>
Run Code Online (Sandbox Code Playgroud)

获取所有生成输入的值的angular2方法是什么?

Dan*_*cal 6

最简单的方法是使用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>
Run Code Online (Sandbox Code Playgroud)

然后,您将可以访问getAllValues()函数中的myForm.form.value对象.Plnkr:https://plnkr.co/edit/84mzcNJliMmvszPq3xMm ? p = preview