angular - Angular 8:如何在没有 html 标签的情况下使用 ngFor。我尝试了 ng-container 但它不起作用
问题描述
在我遇到很多问题后,我创建了以下想法来填补我的 Angular 项目中的空白问题。下面显示的对象来自 API,我想像下面提到的那样循环它。我无法使用ng-container
标签,而不是span
因为它会创建折旧的 html 标签问题。原因是,我在数组中提到的标签不会在同一个对象中关闭,它将被命名为字段的给定数组的下一个对象的下一个或下一个对象关闭。我的意思是,如果第0
th 订单有值<p>my text
并且第1
st 订单将有值is this </p>
。
以下代码有效
<div *ngIf="fill.type == 'lines'" >
<span *ngFor="let f of fill.field">
<span *ngIf="f.word_type == 'word'" [innerHTML]="makeSanitize(f.word)"></span>
<span *ngIf="f.word_type == 'blank'"><input type="text" (keyup)="updateinput($event, f, miniq)" value="" [class.bg-success]="f.myanswer == f.word && miniq.answered && miniq.verified" [class.bg-danger]="f.myanswer != f.word && miniq.answered && miniq.verified" /></span>
</span>
</div>
但是下面的代码给出了一些错误
<div *ngIf="fill.type == 'lines'" >
<ng-container *ngFor="let f of fill.field">
<ng-container *ngIf="f.word_type == 'word'" [innerHTML]="makeSanitize(f.word)"></ng-container>
<ng-container *ngIf="f.word_type == 'blank'"><input type="text" (keyup)="updateinput($event, f, miniq)" value="" [class.bg-success]="f.myanswer == f.word && miniq.answered && miniq.verified" [class.bg-danger]="f.myanswer != f.word && miniq.answered && miniq.verified" /></ng-container>
</ng-container>
</div>
这个循环的 JSON 响应是
type: "lines",
field:
0: {word: "<p>during table - "
word_type: "word"
myanswer: ""},
1: {word: "apple"
word_type: "blank"
myanswer: ""},
2: {word: " answer1</p>
↵
↵<p> - answer2</p>
↵
↵<p> - "
word_type: "word"
myanswer: ""},
3: {word: "orange"
word_type: "blank"
myanswer: ""},
4: {word: " answer3</p>
↵
↵<p> - test
answer4</p>"
word_type: "word"
myanswer: ""}
任何人都请帮助我如何在没有标签的情况下使用它或如何使其ng-container
工作
解决方案
您不能绑定到,innerHTML
因为它不是ng-container
错误消息已经暗示的属性。改用数据绑定,这应该可以解决问题。( {{ makeSanitize(f.word) }} )
<ng-container *ngIf="f.word_type == 'word'">{{ makeSanitize(f.word) }}</ng-container>
<ng-container *ngIf="f.word_type == 'blank'">
<input type="text" (keyup)="updateinput($event, f, miniq)" value="" [class.bg-success]="f.myanswer == f.word && miniq.answered && miniq.verified" [class.bg-danger]="f.myanswer != f.word && miniq.answered && miniq.verified" />
</ng-container>
</ng-container>
推荐阅读
- android - ScrollView 或 NestedScrollView 中的项目一次跳转两个焦点
- excel - 使用 VBA 从 Excel 更新 Access 表。有时会出现“当前记录集不支持更新”
- c# - 使用 namevaluecollection 在数组中设置查询字符串
- php - Cakephp 3.6 - 对每个请求进行自定义身份验证检查
- dygraphs - 带有用于 x 轴值的浮点的 dygraph xy 图
- go - 为什么我的 client.go 无法访问 math.go?
- javascript - 用户每天使用 cookie 访问网站 x 分钟后触发操作
- mysql - Mysql DELETE (with NOT IN subselect) not using index and getting ERROR 1205 (HY000): Lock wait timeout exceeded;
- python - 将一个类与python中的字符串相同地散列
- bash - 替换多行“!” 用一个“!”