I am getting Data in Console Using Angular but I c

2019-09-20 03:12发布

问题:

This is my 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);
      });
  }

This is the response I am getting in the Console. I am getting this Object Object Error and it fails to render it in 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

回答1:

try this,

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);
      });
  }


回答2:

The reason is i am getting this that was i am Returning my data in Object not Array I done it this way like

{{pages.title}} 

and You Can make it to Objects Like this

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