Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 400
UzUmAkI_NaRuTo Avatar asked Oct 03 '15 14:10

UzUmAkI_NaRuTo


People also ask

What does $q do in AngularJS?

$q is integrated with the $rootScope. Scope Scope model observation mechanism in AngularJS, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI.

What is Q defer () in AngularJS?

$q. defer() allows you to create a promise object which you might want to return to the function that called your login function.


1 Answers

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

like image 155
Maxim Shoustin Avatar answered Nov 09 '22 06:11

Maxim Shoustin