7

After Refering this Link , I am trying to get JSON data into my angular service.

Service:

.factory('restservice', ['$rootScope','$http', '$q', '$log',
function($rootScope,$q, $http) {
return {
    getData: function() {
        var defer = $q.defer();
        $http.get('xyz.com/abc.php', { cache: 'true'})
        .success(function(data) {
            defer.resolve(data);
        });

        return defer.promise;
      }
};
}])


Controller:

.controller('RestaurantsCtrl', function ($scope,$http, restservice,restViewservice){

      restservice.getData().then(function(data) {
      $scope.Restaurants = data;
    });

})


After Implementing this console says '$q.defer is not a function'.

What's the issue here? Please Help ...!! Am new to Angular Js so forgive if something is wrong.

Community
  • 1
  • 1
UzUmAkI_NaRuTo
  • 565
  • 3
  • 8
  • 20

2 Answers2

24

You have wrong service definition:

factory('restservice', ['$rootScope','$http', '$q', '$log',
function($rootScope, $q, $http) {

Should be:

factory('restservice', ['$rootScope', '$http', '$q', '$log',
function($rootScope, $http, $q, $log) {

Common mistake :)

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • Thanks a lot ! That was quick ! worked ! i feel stupid for being a noob in angular now. i thought that won't matter. well thanks – UzUmAkI_NaRuTo Oct 03 '15 at 14:31
  • Everyone makes this type of errors, me also, after 2 years of angular. No worries :) – Maxim Shoustin Oct 03 '15 at 14:32
  • @Maxxim Shoustin can you help with cache. the above http get object is not getting cached. I am using anular js to build app with ionic. this default caching wont work in mobile apps ? – UzUmAkI_NaRuTo Oct 03 '15 at 15:13
  • @SumeetDarade on mobiles like Cordova all data should be stored in native (a.e. SharePreferences for Android or UserDefaults for iOS or sqlite if you have a lot of data and need some manipulation). Also since angular is MVC, the model should be defined in native also and not in services like in Web. – Maxim Shoustin Oct 03 '15 at 15:30
  • @SumeetDarade Further, All http calls I suggest you to make in native and not in Angular (aka JS) because if you expect a lot of users, some devices have bugs with sending http data – Maxim Shoustin Oct 03 '15 at 15:32
  • actualy its the matter of one JSON object thats used to populate list. since its just a single object thats of importance i planned on caching it. Cordova has plugins for storage options like sql. But , i didnt wanted to do that just for single json object and was finding a easier way out. about http , ionic uses angular js so i had to – UzUmAkI_NaRuTo Oct 03 '15 at 15:39
  • To clarify this answer further, the inject parameters and function parameters differ. In the answer above, the `$log` was missing from the function parameters. – Joe McLean May 03 '22 at 16:19
0

We faced the same error and resolved now:

Please refer angular 1.6.1 version file, since older version of angular gives the above error.

Amay Kulkarni
  • 828
  • 13
  • 16