Skip to content

Instantly share code, notes, and snippets.

@tedhagos
Created December 18, 2018 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedhagos/3186b85f79d401d02b0b4f8ef104927b to your computer and use it in GitHub Desktop.
Save tedhagos/3186b85f79d401d02b0b4f8ef104927b to your computer and use it in GitHub Desktop.
http observable sample
  1. On the root module (app.module.ts)

    • import the HttpClientModule
    • put it in the imports section
  2. On the app.service.ts

    • import the HttpClient from @angular/common/http
    • Inject it in the constructor of the service
  3. In angular.json

    • Under architect > build > options > assets, add "src/api"
  4. Inside the src folder

    • create a folder named "api"
    • insie the "api" folder, put the file "books.json"
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"http": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/http",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets",
"src/api"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "http:build"
},
"configurations": {
"production": {
"browserTarget": "http:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "http:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets",
"src/api"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"http-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "http:serve"
},
"configurations": {
"production": {
"devServerTarget": "http:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "http"
}
<h1>{{ title }}</h1>
<ul>
<li *ngFor="let b of books">
{{ b.title }} - {{ b.author}}
</li>
</ul>
import { Component, OnInit } from '@angular/core';
import { BookService } from './app.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'http';
books: any[];
errorMessage:any;
constructor(private bookservice: BookService) {}
ngOnInit() {
this.bookservice.getBooks().subscribe(
book => {
this.books = book
},
error => this.errorMessage = <any>error
);
}
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { tap } from "rxjs/operators";
import { HttpClient } from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class BookService {
private bookurl = 'api/books.json';
constructor(private http: HttpClient) {}
getBooks():Observable<any> {
return this.http.get(this.bookurl).pipe(
tap(data => console.log(JSON.stringify(data)))
);
}
}
[
{
"title": "War and Peace",
"author": "Leo Tolstoy",
"price": 15,
"rating": 4.5
},
{
"title": "Nicholas Nickleby",
"author": "Charles Dickens",
"price": 12,
"rating": 3.5
},
{
"title": "Memoirs of Sherlock Holmes",
"author": "Arthur Conan Doyle",
"price": 35,
"rating": 5
},
{
"title": "Scarlet Letter",
"author": "Nathaniel Hawthorne",
"price": 12,
"rating": 3
},
{
"title": "Don Quixote",
"author": "Cervantes",
"price": 20,
"rating": 4
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment