AngularJS : Fetching data from the server

In all previous blogs on AngularJS, we used hard coded data. Now, its the time to get more dynamic and play with the data fetched from the server. For this, we will use AngularJS dependency injection which injects various services into the controller. To use a service is even more simpler. We just declare the name of the service as an argument to the controller function and AngularJS makes that service available for us. To fetch data from the server, we will use $http service.

In line 7, we are using $http service of AngularJS to fetch data from a url and on success event, we assign the data fetched to our local variable using $scope.data=ytdata. Here, the data is a list of youtube videos where every video has an id, a thumbUrl and total number of views and it is in JSON format.

In line 20, we loop over the JSON data using ng-repeat attribute of AngularJS and create clickable thumbnail for every video and display its associated id and number of views.

In line 22, we use ng-src attribute instead of normal src attribute of image tag, so that the AngularJS expression {{datum.thumbUrl}} is evaluated correctly. If we use src, sometimes the AngularJS expression cannot be evaluated correctly and {{datum.thumbUrl}} is placed literally as image source, which is obviously wrong.

Hello! Thank you for this helpfulď»ż video. By the way, I notice lots of people keep on talking about iPhonezilax Secrets (do a google search), but I’m not sure if it is good. Have you tried this step by step guide called iPhonezilax Secrets? I have heard several great things about it and my buddy get his first iphone apps made without any programming experience at all with it, but he refuses to tell me