Get http request on server side works but not display on client side [Angular Universal(universal cli)]

804 views Asked by At

I have request like this:

import {Component, OnInit} from '@angular/core';
import { Http, Response } from '@angular/http';
import {Observable} from 'rxjs';
import 'rxjs/add/operator/map';
import {isNode} from 'angular2-universal';


@Component({
    selector: 'app-todo',
    templateUrl: './todo.component.html',
    styleUrls: ['./todo.component.css']
})
export class TodoComponent implements OnInit {

    constructor(private http: Http) {}
    private todo: any;

    ngOnInit() {
        if(isNode) {
            this.http
            .get('http://api.pushsc.com/api/v1/code/show/589ceab40a58d602a0b99d10')
            .map((response: Response) => response.json())
            .subscribe(data => {
                console.log(data);
                this.todo = data;
            })
            error => {
                console.log(error);
            };
        }

    }

}

In console I get response from API which mean http from server works... But poroblem is on client even if I remove isNode content from http not render as it should... What can be a problem?

2

There are 2 answers

1
Aravind On

From the source code of angular2-universal from this link

By default the value assigned to the isNode is true and so your http call fails if it is not available.

0
grinat On

You need to install and enable preboot https://github.com/angular/preboot for run code on client side.