Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yep this is the Angular way! However, I'd like to suggest some modifications.</p> <p>With your current implementation, each time you inject <code>"DataResults"</code> into a controller, you will be making the <code>$http.get</code> request, on page load. What happens when you need to add different <code>$http.get</code> functions?<br> I would inject the <code>$q</code> service into your <code>"DataResults"</code> factory, which will allow you greater flexibility. Using the <code>$q</code> service with the Deferred API, you not only return a promise, but the promise is resolved with the value itself. </p> <p>Updated factory:</p> <pre><code>var myApp = angular.module("myApp ", []); myApp.factory("DataResults",function($http,$q) { return { getJsonData: function(){ var deferred = $q.defer(); $http.get("/js/data.json"). success(function(d, status, headers, config) { deferred.resolve(_.compact(d)); }). error(function(d, status, headers, config) { deferred.reject(d); }); return deferred.promise; }, getSomeOtherData: function(){ var deferred = $q.defer(); $http.get("/js/data.json"). success(function(d, status, headers, config) { deferred.resolve(_.compact(d)); }). error(function(d, status, headers, config) { deferred.reject(d); }); return deferred.promise; } }; }); </code></pre> <p>updated Controller:</p> <pre><code>myApp.controller("DataSearchCtrl", ["$scope", "DataResults", function($scope, DataResults) { $scope.initializePage = function(){ DataResults.getJsonData().then(function(jsonData) { $scope.data = jsonData; }); }; $scope.loadTheOtherData = function(){ DataResults.getSomeOtherData().then(function(someOtherData){ $scope.otherData = someOtherData; }); }; }]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload