1

I have a problem displaying the data on my View can't display objects iterated on my array. on my console show all objects with no error but I can't display using ngFor

posts.coomponent.ts

import { Component, OnInit } from '@angular/core';
import {Pipe, PipeTransform} from '@angular/core';


import {PostService} from '../posts.service';
import {Post} from '../posts';


@Component({
  selector: 'app-form-posts',
  templateUrl: './form-posts.component.html',
  styleUrls: ['./form-posts.component.css'],
  providers: [PostService]
 })
 export class FormPostsComponent implements OnInit {
 submitted = false;
 posts: Post[] = [];


 constructor(
 private _pService: PostService,
  ) { }

  ngOnInit() {
      this.getAllPost();
  }



  getAllPost() {
   this._pService.getAll().subscribe(
    posts => {
    this.posts = posts  ;
    console.log(this.posts);
    }
  );
  }

  onSave() {
  this.submitted = true;
 }


 }

posts.service.ts

import { Injectable } from '@angular/core';
import {Http, Headers, Response, RequestOptions } from '@angular/http';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/do';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class PostService {
 base: string = 'http://localhost:3000/posts';
 constructor(
    private _http: Http,
 ) {}

 getAll() {
    return this._http.get(this.base)
    .map((response => response.json()));
   }
 }

console results this on my view

<li *ngFor="let post of posts">{{post.categoria}}</li>
3
  • You forgot to use pipe? Commented Mar 17, 2017 at 22:22
  • @Sasxa posts it's an array not an object Commented Mar 17, 2017 at 22:24
  • @jonrsharpe I'm not using the pipe I forgot moved. before ask the question :( Commented Mar 17, 2017 at 22:24

2 Answers 2

3

Typo!

Instead of the plural {{posts.categoria}} use the singular {{post.categoria}} in your <li>

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but not working just showing a empty <li> bullets from the <ul> :(
Try just setting this.JSON equal to window.JSON in the controller (JSON = JSON) then you can do {{JSON.stringify(post)}} in the <li> to get more info.
0

after doing this console.log(this.posts);

what was your response on chrome and i will want you to change this line posts: Post[] = []; to this public posts: any = []; in the posts.coomponent.ts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.