简体   繁体   English

如何访问对象内部的数组并且数组本身包含有角度的对象?

[英]how to access array inside an object and the array itself contain objects in angular?

**I am trying to print the detail of Courses in the DOM but i am getting an error. **我正在尝试在 DOM 中打印课程的详细信息,但出现错误。 I want to print the detail of courses which is shown in the below image.我想打印下图所示的课程详细信息。 ** **

This is the service file which is fetching the data from the API.这是从 API 获取数据的服务文件。

services.service.ts服务.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Category } from '../models/category.model';
import {map} from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class ServicesService {
  category:Category[]=[];
 uri:string='https://localhost:44347/api';
  constructor(private http:HttpClient) { }

  public fetchCategory(categoryId){
    return this.http.get<Category[]>(`${this.uri}/Categories/${categoryId}`).pipe(map((data: Category[]) => {
      return this.category = data;
    }))
  }
}

In this component i call the service and console log the file which is shown in image below.在这个组件中,我调用服务并控制台记录下图所示的文件。

webdev.component.ts webdev.component.ts

import { Component, OnInit } from '@angular/core';
import { NavService } from 'src/app/shared/nav.service';
import {NavbarComponent} from '../../navbar/navbar.component';
import { Observable, pipe } from 'rxjs';
import { Category } from 'src/app/models/category.model';
import { ServicesService } from 'src/app/shared/services.service';
import {map} from 'rxjs/operators';
@Component({
  selector: 'app-webdev',
  templateUrl: './webdev.component.html',
  styleUrls: ['./webdev.component.css']
})
export class WebdevComponent implements OnInit {
  category=[];
  constructor(private nav:NavService, private service:ServicesService) { }

  ngOnInit(): void {
      this.service.fetchCategory(1).subscribe((cat:Category[])=>{
        this.category=cat;
        console.log(this.category);
      })

}
}

This my template file in angular.这是我的 angular 模板文件。 webdev.component.html webdev.component.html

<ng-container  class="cntr">
   <div *ngFor="let docc of category; let i=index" class="cntr">
      <div *ngFor="let doc of docc.courses">
 <div class="card text-center" style="width: 18rem;">
    <img src="{{docc.imageUrl}}" class="card-img-top w-50 m-auto" alt="HTML5">
   <div class="card-body">
     <h5 class="card-title text-center">{{doc.courseName}}</h5>
     <p class="card-text text-center">{{doc.courseDescription}}</p>
      <p class="class-text font-weight-bold"><del><span>&#36;</span>{{doc.price}}</del></p>
     <a href="#" class="btn btn-primary text-center" routerLink="/web">Start Course</a>
   </div>
 </div>
 </div>
</div>
 </ng-container> 

https://i.imgur.com/XFERDUA.png https://i.imgur.com/XFERDUA.png

You do not need the first *ngFor as the category is an object.您不需要第一个*ngFor因为category是一个对象。 Since you only need the courses array contained within the category variable, you could directly loop over by using *ngFor="let doc of category.courses" .既然你只需要在courses包含的内阵列category的变量,可以直接循环可以在使用*ngFor="let doc of category.courses" Try the following尝试以下

<ng-container *ngIf="category" class="cntr">
  <div *ngFor="let doc of category.courses">
    <div class="card text-center" style="width: 18rem;">
      <img src="{{docc.imageUrl}}" class="card-img-top w-50 m-auto" alt="HTML5">
      <div class="card-body">
        <h5 class="card-title text-center">{{doc.courseName}}</h5>
        <p class="card-text text-center">{{doc.courseDescription}}</p>
        <p class="class-text font-weight-bold"><del><span>&#36;</span>{{doc.price}}</del></p>
        <a href="#" class="btn btn-primary text-center" routerLink="/web">Start Course</a>
      </div>
    </div>
  </div>
</ng-container> 

However if you also wish to retrieve properties other than courses from the category object, then you could use keyvalue pipe.但是,如果您还希望从category对象中检索courses以外的属性,则可以使用keyvalue管道。

<ng-container *ngIf="category" class="cntr">
  <div *ngFor="let cat of category | keyvalue">
    <ng-container *ngIf="cat.key !== 'courses';else courses>
      <p>{{cat.key}}: {{cat.value}}</p>
    </ng-container>
    <ng-template #courses>
      <div *ngFor="let doc of cat.value">
        <div class="card text-center" style="width: 18rem;">
          <img src="{{docc.imageUrl}}" class="card-img-top w-50 m-auto" alt="HTML5">
          <div class="card-body">
            <h5 class="card-title text-center">{{doc.courseName}}</h5>
            <p class="card-text text-center">{{doc.courseDescription}}</p>
            <p class="class-text font-weight-bold"><del><span>&#36;</span>{{doc.price}}</del></p>
            <a href="#" class="btn btn-primary text-center" routerLink="/web">Start Course</a>
          </div>
        </div>
      </div>
    </ng-template>
  </div>
</ng-container> 

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

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