html - 如何在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>
我需要一个应该返回数组值的方法,但我得到的是空白数组值
解决方案
从 HTML 到 .ts 文件中获取 formarray 值或任何值非常容易。您需要做的是,在您的 HTML 文件中编写此代码:-
{{log(formarray)}} // or any value you want to log in console.
在您的 .ts 文件中,您可以编写此函数:-
log(value) {
console.log(value);
}
推荐阅读
- python - PyLint W0143 警告:与可调用对象进行比较
- javascript - 为什么从 div 扩展的自定义元素在样式方面不像 div
- spring - Spring自定义查询转义:
- javascript - 同时加载多个图像
- r - R shiny - 用户定义的函数在 mainPanel 中打印
- oracle - 如何在 Docker 中连接到外部 Oracle 数据库
- php - 如何在 Symfony Messenger 的中间件上禁用日志“信息”?
- algorithm - 如何使用机器学习计算文本中的单词
- javascript - 如何通过特定对象键对数组中的值求和?
- python - 使用 PyTorch LSTM,我的 hidden_size 可以与 input_size 不同吗?