首页 > 解决方案 > 如何在angular的打字稿文件中访问formarray值

问题描述

我无法访问打字稿文件中的表单数组值。我把它当作空白数组这是我的 html 代码

  <mat-form-field class="example-full-width" >
                    <mat-label>Locations </mat-label>
                    <mat-chip-list #chipList aria-label="Fruit selection">
                      <mat-chip
                        *ngFor="let loca of locationlist"
                        [selectable]="selectable"
                        [removable]="removable"
                        (removed)="remove(loca)">
                        {{loca.locationName}}
                        <mat-icon matChipRemove (click)="remove(loca)">cancel</mat-icon>
                      </mat-chip>
                      <input
                        placeholder="Locations"
                        #fruitInput
                        formArrayName="locationctrl"
                        [matAutocomplete]="auto"
                        [matChipInputFor]="chipList"
                        [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                        (matChipInputTokenEnd)="add($event)">
                    </mat-chip-list>
                    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
                      <mat-option *ngFor="let locc of locations " [value]="locc"> {{locc.locationName}}   </mat-option>
                    </mat-autocomplete>
                </mat-form-field>

我需要一个应该返回数组值的方法,但我得到的是空白数组值

标签: htmlangulartypescript

解决方案


从 HTML 到 .ts 文件中获取 formarray 值或任何值非常容易。您需要做的是,在您的 HTML 文件中编写此代码:-

 {{log(formarray)}} // or any value you want to log in console.

在您的 .ts 文件中,您可以编写此函数:-

 log(value) {
   console.log(value);
 }

推荐阅读