简体   繁体   English

如何在 Angular 中使用带有键值 pipe 的插入循环

[英]How to use inserted loops with keyvalue pipe in Angular

My Answer class我的答案 class

import { Question } from './question';

export class Answer {
    AnswerId: number;
    Content: string;
    IsCorrect: boolean;
    Mark: number;
    QuestionId: number;
    Question: Question;
}

My TestStartComponent我的 TestStartComponent

    testInfo: Map<string, Answer[]>;
    id: number;
    loaded: boolean = false;

    constructor(private dataService: DataService, activeRoute: ActivatedRoute) {
        this.id = Number.parseInt(activeRoute.snapshot.params["id"]);
    }

    ngOnInit() {
        if (this.id)
            this.dataService.getTestStart(this.id)
                .subscribe((data: Map<string, Answer[]>) => {
                    this.testInfo = data; this.loaded = true;});
    }

My test-start.component.html我的 test-start.component.html

<div *ngIf="loaded">
    <table class="table table-striped">
        <thead>
            <tr>
                <td>Вопрос</td>
                <td>Вариант 1</td>
                <td>Вариант 2</td>
                <td>Вариант 3</td>
                <td>Вариант 4</td>
                <td>Вариант 5</td>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let item of testInfo | keyvalue">
                <td class="row-number-column">{{item.key}}</td>
                <td>{{item.value}}</td>
                <!--<td *ngFor="let s of testInfo[item] | keyvalue">
                    {{s.content}}
                </td>-->
                <!--<template *ngFor="let s of testInfo[item] | keyvalue">
                    <td>
                        {{s.content}}
                    </td>
                </template>-->
            </tr>
        </tbody>
    </table>
</div>

I want to see all key and values for them in my test-start.component.html, but I cant use nested(inserted) loop for outputing values.我想在我的 test-start.component.html 中查看它们的所有键和值,但我不能使用嵌套(插入)循环来输出值。

So, the question is, how to output all values for every key?所以,问题是,如何 output 每个键的所有值?

keyvalue pipe is for iterating through object properties, not for arrays.键值 pipe 用于遍历 object 属性,不适用于 arrays。 You have an array of Answers, so you can simply pass it to the ngFor.你有一个答案数组,所以你可以简单地将它传递给 ngFor。 If you want however, you can print all the key-values of an Answer as well.但是,如果您愿意,您也可以打印答案的所有键值。

<tr *ngFor="let answer of testInfo">
  <td class="row-number-column">{{answer.answerId}}</td>
  <td>{{answer.content}}</td>
  <td *ngFor="let item of answer| keyvalue">
    {{item.key}} : {{item.value}} <br>
  </td>
</tr>

That how I solve my problem那我如何解决我的问题

            <tr *ngFor="let item of testInfo | keyvalue">
                <td class="row-number-column">{{item.key}}</td>
                <td *ngFor="let s of testInfo[item.key]">
                    {{s.content}}
                </td>
            </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM