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

After linking to Link , I am trying to get JSON data in my angular service.

Services:

.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, it says ' $ q.defer is not a function .

What is the problem here? Please help ... !! I am new to angular Js, so sorry if something is wrong.

+6
source share
2 answers

You have the wrong service definition:

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

Must be:

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

Common mistake :)

+21
source

We encountered the same error and decided now:

Please refer to the angular version 1.6.1 file, as an earlier version of angular gives the above error.

0
source

All Articles