我正在使用Angular在控制台中获取数据,但无法在html

时间:2018-06-23 19:31:22

标签: node.js angular api angular6

这是我的Page-datail.component.ts

export class PageDetailComponent implements OnInit {

  private apiUrl="http://localhost:3000/pages"

  pages={};
  // data={};

  constructor(private route:ActivatedRoute,private page:PageService,private router: Router,private http:Http) { }

  ngOnInit() {
    this.getpage(this.route.snapshot.params['title']);
    // this.getPages();
    // this.getData();

  }
  getpage(title) {
    this.page.getPage(title)
      .subscribe(pages => {
        console.log(pages);
        this.pages = pages;
      }, err => {
        console.log(err);
      });
  }

这是我在控制台中得到的响应。我收到此“对象对象错误”,它无法用html呈现。

<a *ngFor="let p of pages" class="list-group-item list-group-item-action">
  {{p.title}}
</a>

This is the response I am getting in the console

This is the response I am getting in the console

2 个答案:

答案 0 :(得分:0)

原因是我得到的是返回对象而不是数组中的数据的方式,就像

{{pages.title}} 

,您可以将其设置为这样的对象

pages:any={};   //For Objects
page:any=[];    //For Arrays

答案 1 :(得分:0)

尝试一下

export class PageDetailComponent implements OnInit {

  private apiUrl="http://localhost:3000/pages"

  pages=[];
  // data={};

  constructor(private route:ActivatedRoute,private page:PageService,private router: Router,private http:Http) { }

  ngOnInit() {
    this.getpage(this.route.snapshot.params['title']);
    // this.getPages();
    // this.getData();

  }
  getpage(title) {
    this.page.getPage(title)
      .subscribe(page => {
        console.log(page);
        this.pages.push(page);
      }, err => {
        console.log(err);
      });
  }