Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Created July 17, 2018 13:43
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 christiannagel/b469403ae021f9047156990daa8251ca to your computer and use it in GitHub Desktop.
Save christiannagel/b469403ae021f9047156990daa8251ca to your computer and use it in GitHub Desktop.
Angular Books Component - getting books from an Web API
import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-books',
templateUrl: './books.component.html',
styleUrls: ['./books.component.css']
})
export class BooksComponent {
public books: Book[];
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<Book[]>(baseUrl + 'api/BooksFunction').subscribe(result => {
this.books = result;
console.log("retrieved" + this.books);
console.log("title: " + this.books[0].title);
}, error => console.error(error));
}
}
interface Book {
bookId: number;
title: string;
publisher: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment