2 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
{
"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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
不完全確定您要做什么,但似乎數(shù)組實(shí)際上位于嵌套屬性內(nèi)data。您可以Array#map僅提取特定屬性并async在模板中使用管道來(lái)避免訂閱。
嘗試以下操作
控制器
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
更新:迭代控制器中的數(shù)組
要在控制器中使用響應(yīng),您可以跳過(guò)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
添加回答
舉報(bào)