angular2 ngFor not working while fetching data from api on ngOnInit()

后端 未结 6 465
别跟我提以往
别跟我提以往 2021-01-13 03:42

comment.component.ts:

import { Component, OnInit } from \'@angular/core\';
import { Router} from \'@angular/router\'
import { Comment } from         


        
6条回答
  •  时光说笑
    2021-01-13 04:43

    Try adding the ngFor in the ul tag

    • {{comment.Name}}
    ` or
    • {{comment.Name}}
    `

    also maybe you have not pasted your entire code, i don't see a injection of the service component.

    you will need to import and inject the service in the component that you want to use it in. In the component constructor

        import {CommentService} from '../path to service'
    
        constructor(private commentService: CommentService) 
        {
    
        //you can call the getcomments service here itself or in ngOnInit
        this.commentService.getComments().subscribe(data =>    
                     {    
                       console.log(data)
                      }
        } //end constructor
    

    Hopefully you have tested that your commentService is returning data via console.log

提交回复
热议问题