Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 7, 2017 17:26
Show Gist options
  • Save tkssharma/71d70c57ebea1cd0d9aee30475bf2fb5 to your computer and use it in GitHub Desktop.
Save tkssharma/71d70c57ebea1cd0d9aee30475bf2fb5 to your computer and use it in GitHub Desktop.
@Injectable()
export class CommentService {
// Resolve HTTP using the constructor
constructor (private http: Http) {}
// private instance variable to hold base url
private commentsUrl = 'http://localhost:3000/api/comments';
}
getComments() : Observable<Comment[]> {
// ...using get request
return this.http.get(this.commentsUrl)
// ...and calling .json() on the response to return data
.map((res:Response) => res.json())
//...errors if any
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment