1

Having some issues with getting the observable Array on my component receiving the data from my service.

The service loads fine, and I can see the objects. But the Component is not loading the data into the Blog Array.

Service Logic:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/Rx'; 
import { Observable }     from 'rxjs/Observable';

import { Blog } from '../models/blog.ts';


@Injectable()

export class BlogService {

    private blogsUrl:string = '/blog.json'

    constructor(
        private _http: Http
        ){}

    getBlogs(): Observable<Blog[]>{
        return this._http.get(this.blogsUrl)
            .map(this.extractData)
            .catch(this.handleError);
    }

    private extractData(res: Response) {
        let body = res.json();
        console.log(body);
        return body || {};
    }

    private handleError(error: any) {
        // In a real world app, we might use a remote logging infrastructure
        // We'd also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }

}

Component Logic:

import { Component, OnInit } from '@angular/core';
import { config } from '../config';
import { BlogService } from './services/blog.srv'
import { Blog } from './models/blog';

@Component({
    selector: 'blog-component',
    templateUrl: config.templateUrl + './blog/views/blog.component.html',
    styleUrls: ['/assets/js/app/blog/styles/blog.component.css'], 
    providers: [
        BlogService
    ]
})

export class BlogComponent implements OnInit{
    public page = 'Blog';
    errorMessage: string;
    blogs: Blog[];
    mode = 'Observable';

    constructor(
        private _blogSrv: BlogService
            ){}

    ngOnInit() { 
        this.getBlogs();
    }

    getBlogs(){
        return this._blogSrv.getBlogs()
            .subscribe(
            blogs => this.blogs = blogs,
            error => this.errorMessage = <any>error);
    }

}

Am I misunderstanding how the observable works? Should it not load the blogs object with the return "body"

EDIT:

Main.ts:

/// <reference path="../typings/browser.d.ts" />

import { bootstrap } from '@angular/platform-browser-dynamic';
import { ROUTER_PROVIDERS } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';
import { AppComponent } from './app.component';


bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    HTTP_PROVIDERS
]); 
3
  • Do you have any error when execute your HTTP request? Commented Jun 11, 2016 at 18:44
  • Does console.log(body); print something in the JavaScript console? Commented Jun 11, 2016 at 18:45
  • it does, it logs the array of blog objects. Commented Jun 11, 2016 at 20:35

2 Answers 2

1

Perhaps you forgot to specify HTTP_PROVIDERS when bootstrapping your application:

bootstrap(AppComponent, [ HTTP_PROVIDERS ]);

Otherwise, the code of both component and service looks fine.

Edit

Perhaps you could force Angular 2 to detect changes:

@Component({
  (...)
})
export class BlogComponent implements OnInit{
  public page = 'Blog';
  errorMessage: string;
  blogs: Blog[];
  mode = 'Observable';

  constructor(
    private _blogSrv: BlogService,
    private _cdr:ChangeDetectorRef
        ){}

  ngOnInit() { 
    this.getBlogs();
  }

  getBlogs(){
    return this._blogSrv.getBlogs()
        .subscribe(
        blogs => {
          this.blogs = blogs;
          this._cdr.detectChanges(); // <------
        },
        error => this.errorMessage = <any>error);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I did, It's injecting the providers at the bootstrap, but I will add my main.ts
Since you received data in your service, you could perhaps force Angular 2 to detect changes... I updated my answer accordingly.
Hmm still nothing. The weird thing is that a console.log(blogs) in the component subscribe logs the object array as well.
Turns out this was an issue with the view and not the component.
0

The issue here is that the view was not properly copied down to the public directory. with the new *ngFor.

Also the array was not being set to empty when declare so:

blogs:Blog[]; vs blogs:Blog[] = []

Now it works, verified with another section which uses similar logic.

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.