UzUmAkI_NaRuTo
UzUmAkI_NaRuTo

Reputation: 575

Angular Js : '$q.defer is not a function' error

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.

Upvotes: 8

Views: 12580

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

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 :)

Upvotes: 25

Amay Kulkarni
Amay Kulkarni

Reputation: 838

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.

Upvotes: 0

Related Questions