I created a very simple app based on the Angular2 tutorial.
To start, I have a very simple "Book" model:
/**
* book model
*/
export class Book {
public data;
/**
* constructor
* @param id
* @param title
* @param pages
*/
constructor(
public id,
public title:string,
public pages:Array
){
alert('it works'); // just a check
}
}
In my service, I get a book like this:
return this._http.get('getBook/1')
.map(function(res){
return <Book> res.json();
})
My expectation was that this would get the resulting JSON data and "map" it to a Book object.
However, it just returns an object with type "Object".
I could create a new Book object myself and pass the parameters in the constructor, like this:
return new Book(res.id, res.title, res.pages);
Is this the best way to do this? Did I miss something?