Oct 20, 2015
This blog will continue on from my previous blog post, here, we will create an application that uses actual data from a specified source. The source of our data will be “Spotify”, this will give me the ability to retrieve any artist information that I might want to use.
The goal of this application is to provide the user with a search field so that they can retrieve any information on the artist in question. We will be utilising the Web API that Spotify exposes to retrieve information, and this will be configured in the Angular Service that will be set up as well.
We will leave all the components that was previously created in place and just add additional files as we need them. Firstly we need to understand how to communicate to the source of where we are fetching the information from, this can be done by visiting the following link: https://developer.spotify.com/web-api/.
- Name the file something appropriate such as “MusicApp”
- Now create the new Angular Module inside this app
The result should now look like the following:
Now we will create an Angular Service to do the actual retrieval of the data, this service will be called from our controller that we will create in a later stage. To use the Web API we need to form a REST request, the easiest way for me to do this is to create a service and use the $HTTP object in angular. The steps needed to implement an Angular JS Service is as follows:
- Create a service using the Angular factory method, you can use the service method if you want. The factory method is however the recommended way.
a) On line 3 we give the service a name and pass in any dependencies that the service might use, such as the $http object to make web requests
b) We then create an empty object to represent the service and to return it to the calling controller
Once the service is created we need to add a function to the service to do the actual request for the data of interest:
- Within the service create a new function and pass in “artistname” as the argument to the function. (The user will search by artist name)
- Within the function use the $http object to form a request to the Web API
a) Specify a URL that matches the Web API documentation
b) Include the argument within the URL you just specified
c) Make sure the pass in a method of “GET” as we are retrieving results
The service alone doesn’t do much at the moment, to change that we will add a controller to make calls to the service. This controller will contain the needed method for the user to invoke when trying to search an artist. Perform the following steps to create the controller:
- After the factory method you specified earlier, create a new controller by using the controller function. I named mine “ArtistController”
- Inject both the ArtistService and $scope objects as dependencies
- Within the controller add a function to the scope object so that the user can call it.
a) Create a property to save the artistname.
b) This function has an argument of “artistname” and will call the method within the service using a “then” method.
c) The “then” method has two callback methods, one for success and one for error.
d) Within the success method, retrieve the information from the response to represent your artist. Once again the properties that are returned is in the Web API documentation
e) Within the error method show an appropriate error message to the user.
Once you have created the controller, the last thing remaining is to create a proper page to display the information as well as a search component. We will add a new HTML file to our application and call it “ArtistInfo”. Once the page is created we need to do the following:
- Add bootstrap to the Project and references to the page, so that it is better to look at.
Now create the elements on the page as you see fit. Some of these elements need some extra properties to showcase the values retrieved from Spotify. The following image is my end result HTML.
Take note of the following element in regards to the previous image:
- Firstly we have an input element on line 18 that uses the “ng-model” directive to bind to a property called “artistname”.
- Next, we have a button element on line 19 that uses the “ng-click” directive to bind to a function in the controller called “getarts”.
- Lines 23 and 26 uses the “ng-if” directive to check if there is any results returned and then based on the outcome it will either show the header for results or a header for no results.
- Line 29 uses a directive called “ng-repeat” , this directive will loop through a collection of items and create the nested elements for each one of those items.
a) We are retrieving multiple artists, so we want to show an item box for each artist.
b) Each item is placed within the art object
c) Within the ”ng-repeat” element we refer to the art object to access the properties of each artist. In my case I am showing an image, the artist name and the amount of followers.
Once you have this in place and you run the application you should see the following:
Your result will vary depending on your styling. Finally when you search for an artist you should either see a header saying no results or you should see the artists appear.