2 回答
TA贡献1796条经验 获得超10个赞
{
"data": [
{
"id": 18,
"internal_name": "scone_birthday",
"type": "coupon",
"description": "Free Scone awarded for a members birthday",
"heading": "Toast to your birthday!",
"subheading": "Our gift to you",
"body": "This is the body for the <span style='font-family: \"Times New Roman\", Times, serif;'><strong><em><span style=\"color: rgb(0, 198, 255);\">birthday offer.</span></em></strong></span><span style=\"font-family: Georgia,serif;\"></span>",
"subject": "",
"details": "This is the details for the birthday <strong>offer</strong>.",
"action": "",
"image1_bg": "",
"image_url": "images/birthday_candle.png",
"respondable": true,
"metric_amount": "150.0",
"metric_name": "spend",
"usage_end_date": "2020-11-05T23:59:59.000+11:00"
},
{
"id": 4,
"internal_name": "voucher",
"type": "coupon",
"description": null,
"rank": "1.0",
"start_date": null,
"end_date": null,
"heading": "HighFIVE!",
"subheading": "You've got $5 of dough*",
"body": "Redeem for a $5 reward. <a href=\"https://en.wikipedia.org/wiki/Lorem_ipsum\" rel=\"noopener noreferrer\" target=\"_blank\">Lorem ipsum</a> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"subject": "Subject",
"details": "",
"action": "",
"image1_bg": "",
"image_url": "images/five_dollar.png",
"respondable": true,
"metric_amount": "200.0",
"metric_name": "point",
"usage_end_date": "2020-11-08T23:59:59.000+11:00"
},
{
"id": 19,
"internal_name": "loaf_welcome",
"type": "coupon",
"description": "Onboarding offer for member - free loaf ",
"start_date": null,
"end_date": null,
"heading": "Get a slice of delight",
"subheading": "Treat Yourself",
"body": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"subject": "",
"details": "",
"action": "",
"image1_bg": "",
"image_url": "images/gift.png",
"respondable": true,
"metric_amount": "100.0",
"metric_name": "spend",
"usage_end_date": "2020-12-30T23:59:59.000+11:00"
}
]
}
TA贡献1856条经验 获得超17个赞
不完全确定您要做什么,但似乎数组实际上位于嵌套属性内data。您可以Array#map仅提取特定属性并async在模板中使用管道来避免订阅。
尝试以下操作
控制器
import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
@Component({ ... })
export class HomeComponent implements OnInit {
dataValues$: Observable<any>;
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataValues$ = this.http.get('assets/rewards.json')
.getRewards()
.pipe(
map((data: any) => (data.data as Array<any>).map((d: any) => d.body))
);
}
}
模板
<ng-container *ngIf="(dataValues$ | async) as dataValues">
<table class="table table-striped">
<thead>
<tr>
<td>Data Values 1</td>
<td>Data Values 2</td>
<td>Data Values 3</td>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor="let column of dataValues">
<span [innerHTML]="column | safeHtml"></span>
</td>
</tr>
</tbody>
</table>
</ng-container>
安全管
import { Pipe, PipeTransform } from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";
@Pipe({ name: "safeHtml", pure: true })
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(value: string): any {
return this.sanitizer.bypassSecurityTrustHtml(value);
}
}
工作示例:Stackblitz
更新:迭代控制器中的数组
要在控制器中使用响应,您可以跳过async管道并在控制器中订阅。
尝试以下操作
export class HomeComponent implements OnInit {
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataService.getRewards().subscribe({
next: (data: any) => {
data.data.forEach((item: any) => {
console.log(item);
console.log("id:", item.id);
console.log("internal_name:", item.internal_name);
// do something else
});
}
});
}
}
工作示例:Stackblitz
添加回答
举报