angular - 如何为响应式表单设置模板上下文?
问题描述
我在组件中有一组FormGroup
称为fgList
初始化的组。像这样:
const fgList = [
new FormGroup({ /* ... */),
new FormGroup({ /* ... */)
];
对于模板,我想使用*ngFor与ng-template关联,可以吗?
<div *ngFor="let fg of fgList">
<ng-container *ngTemplateOutlet="colorText; context: fg"></ng-container>
</div>
<ng-template #colorText>
<span class="red">{{ fg.get('name').value }}</div>
</ng-template>
它一直说无法读取 undefined 的属性'get'。我可以知道如何fg
在模板中传递和使用它吗?
解决方案
因为你的*ngFor如果关闭。
尝试使用以下代码:
<div *ngFor="let fg of fgList">
<ng-container *ngTemplateOutlet="colorText; context: fg"></ng-container>
<ng-template #colorText>
<span class="red">{{ fg.get('name').value }}</div>
</ng-template>
</div>
推荐阅读
- node.js - 如何将多个数组传递给ejs文件然后遍历它
- c# - Identity Server 4 - 多次调用 GetProfileDataAsync,导致多个数据库条目
- php - Laravel分页不刷新页面
- react-native - 在 react-native-datepicker 中更改语言
- java - 在没有 Windows 变量的情况下运行 Maven?
- python - 调整孪生轴的大小以绘制不同类型的数据与公共参数的关系
- apache-camel - 骆驼文件消费者可以读取 Windows 远程目录吗?
- flutter - 常见的 Snackbar 不显示
- linq - LINQ EF Core 3.0 中的重大更改。使用表达式
- spring-security - 未收到 JWT 不记名令牌?