javascript - Why and how to use Angular2/Typescript model? -
this first post , ask on topic can't seem find info of.
i've been learning angular 2 trough guide , ng-book creating "spotifyapp", , came across best practice create typescript class model of artist , album, import component , set result of http request model.
now don't see difference regular array, don't know why it, , can't seem find relevant info topic. works can't see does. tried break in example below giving random attributes, , no difference
i went angular official docs, , didn't see difference, gone trough typescript tutorial on tutorials point , doesn't mention it. question why need model, use wrong? , use in application?
artist.model.ts
import { album } './album.model' export class artist{ id: number; name: number; peackock: string; genres: any; albums: album[]; } album.model.ts
export class album { id: number; } spotify-app.service
@injectable() export class spotifyappservice { constructor(private http: http) { } searchbytrack(query: string) { let params: string = [ `q=${query}`, `type=artist` ].join("&"); let queryurl = `https://api.spotify.com/v1/search?${params}`; return this.http.get(queryurl).map(res => res.json()); } } spotify-app.component.ts
export class spotifyappcomponent implements oninit { searchstr: string; artist: artist[]; constructor(private spotifyservice: spotifyappservice) { } searchmusic(query: string):void{ this.spotifyservice.searchbytrack(query) .subscribe( res => { this.artist = res.artists.items; }); }; ngoninit() { } }
it's object. , have noticed can have other attributes , still work because forgiving. 1 obvious benefit code completion , error detection. open code in visual studio code , can control click definition.
edit: said gives code completion. ide know when start typing n keep going , fill in name. gives place attributes should expect. can add functions it. give more flexibility passed api.
Comments
Post a Comment