this is my first post and i would like to ask you for help on topic i can't seem to find any info of.
I've been learning Angular 2 trough a guide and ng-book creating "SpotifyApp", and i came across the best practice to create typescript class model of Artist and Album, import it into my component and set result of HTTP request to that Model.
Now i don't see any difference from regular array, i don't know why I do it, and I can't seem to find any relevant info to the topic. It works but I can't see what it does. I even tried to break it like in example below giving it random attributes, and no difference
I went to Angular official docs, and i didn't see any difference, gone trough Typescript tutorial on tutorials point and doesn't mention anything about it. So my question is why i need model, do i use it wrong? And what do I use it for in application?
artist.model.ts
import { Album } from './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() {
}
}