Stav Alfi Stav Alfi - 8 months ago 61
TypeScript Question

how to get data using Http.get from my project?

I got a ts file that contain some users:


import {User} from "../user";

export const USERS:User[]=[
new User(....);

A serice - quickstart-muster/app/services/user.service.ts:

import { Injectable } from '@angular/core';
import {Observable} from "rxjs/Observable"
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/toPromise';
import {Http} from "@angular/http";
import {User} from "../user";

export class UserService {

constructor(private http:Http){}

public loadUsers():Observable<User[]>{
return this.http.get('../mock/user.mock.ts')

I know thats not gonna work and its not, i get 404 error that say he can't find .../mock/user.mock.ts.

How do I make it work?


You could load a JSON file instead like it would come from the API:


        "id": 1234,
        "name": "...",

and then you can load it like this:

public loadUsers(): Observable<User[]>{
  return this.http.get('data.json')
    .map(res => res.json());

You could also create an Observable from scratch like this:

public loadUsers(): Observable<User[]> {
    return Observable.create((observer) => {