{"id":7844,"date":"2012-09-21T11:52:28","date_gmt":"2012-09-21T06:22:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7844"},"modified":"2017-08-03T14:06:27","modified_gmt":"2017-08-03T08:36:27","slug":"angularjs-fetching-data-from-the-server","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/","title":{"rendered":"AngularJS : Fetching data from the server"},"content":{"rendered":"<p>In all <a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">previous blogs<\/a> 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 <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">use AngularJS<\/a> 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 <strong>$http<\/strong> service.<\/p>\n<p>[java]<br \/>\n&amp;lt;!Doctype html&amp;gt;<br \/>\n&amp;lt;html ng-app&amp;gt;<br \/>\n&amp;lt;head&amp;gt;<br \/>\n&amp;lt;script type=&amp;quot;text\/javascript&amp;quot; src=&amp;quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.0.1\/angular.min.js&amp;quot;&amp;gt;&amp;lt;\/script&amp;gt;<br \/>\n&amp;lt;script type=&amp;quot;text\/javascript&amp;quot;&amp;gt;<br \/>\n\tfunction YoutubeController($http,$scope){<br \/>\n        $http.get(&#8216;ytdata.json&#8217;).success(function(ytdata) {<br \/>\n    \t\t\t\t$scope.data = ytdata;<br \/>\n  \t\t});<br \/>\n\t}<br \/>\n&amp;lt;\/script&amp;gt;<br \/>\n&amp;lt;style type=&amp;quot;text\/css&amp;quot;&amp;gt;<br \/>\n\tli{list-style:none;float:left;padding:20px;}<br \/>\n\ta{text-decoration:none;}<br \/>\n&amp;lt;\/style&amp;gt;<br \/>\n&amp;lt;\/head&amp;gt;<br \/>\n&amp;lt;body&amp;gt;<br \/>\n&amp;lt;div ng-controller=&amp;quot;YoutubeController&amp;quot;&amp;gt;<br \/>\n\t&amp;lt;ul&amp;gt;<br \/>\n\t\t&amp;lt;li ng-repeat=&amp;quot;datum in data&amp;quot;&amp;gt;<br \/>\n\t\t\t&amp;lt;a href=&amp;quot;http:\/\/www.youtube.com\/v\/{{datum.id}}&amp;quot;&amp;gt;<br \/>\n\t\t\t\t&amp;lt;img ng-src=&amp;quot;{{datum.thumbUrl}}&amp;quot; \/&amp;gt;<br \/>\n\t\t\t\t&amp;lt;br \/&amp;gt;Id : {{datum.id}}<br \/>\n\t\t\t\t&amp;lt;br \/&amp;gt;Views : {{datum.views}}<br \/>\n\t\t\t&amp;lt;\/a&amp;gt;<br \/>\n\t\t&amp;lt;\/li&amp;gt;<br \/>\n\t&amp;lt;\/ul&amp;gt;<br \/>\n&amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;\/body&amp;gt;<br \/>\n&amp;lt;\/html&amp;gt;<br \/>\n[\/java]<\/p>\n<p>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.<\/p>\n<p>[javascript]<br \/>\n[<br \/>\n\t\t\t{<br \/>\n\t\t\t\t&amp;quot;id&amp;quot;:&amp;quot;yZxrao3zou4&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;thumbUrl&amp;quot;:&amp;quot;http:\/\/img.youtube.com\/vi\/yZxrao3zou4\/default.jpg&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;views&amp;quot;:20000<br \/>\n\t\t\t},<br \/>\n\t\t\t{<br \/>\n\t\t\t\t&amp;quot;id&amp;quot;:&amp;quot;wqQ6BF50AT4&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;thumbUrl&amp;quot;:&amp;quot;http:\/\/img.youtube.com\/vi\/wqQ6BF50AT4\/default.jpg&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;views&amp;quot;:249<br \/>\n\t\t\t},<br \/>\n\t\t\t{<br \/>\n\t\t\t\t&amp;quot;id&amp;quot;:&amp;quot;_oyI0C2kBRc&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;thumbUrl&amp;quot;:&amp;quot;http:\/\/img.youtube.com\/vi\/_oyI0C2kBRc\/default.jpg&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;views&amp;quot;:3833<br \/>\n\t\t\t},<br \/>\n\t\t\t{<br \/>\n\t\t\t\t&amp;quot;id&amp;quot;:&amp;quot;ExW4FisGlYk&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;thumbUrl&amp;quot;:&amp;quot;http:\/\/img.youtube.com\/vi\/ExW4FisGlYk\/default.jpg&amp;quot;,<br \/>\n\t\t\t\t&amp;quot;views&amp;quot;:54355<br \/>\n\t\t\t}<br \/>\n]<br \/>\n[\/javascript]<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>The final output looks something like :<\/p>\n<p>I will be back with more on AngularJS.<br \/>\nTill then, Have Fun. \ud83d\ude42<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS : A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS : Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS : Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS : Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS : Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\"> AngularJS : Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS : Multiple Views, Layout Template and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS : Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1439,3429],"tags":[3955,955,4697,996,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7844"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7844"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7844\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}