Angular Js:'$ q.defer不是函数'错误

时间:2022-12-04 16:20:11

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

在引用此链接之后,我正在尝试将JSON数据放入我的角度服务中。

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'.

实现此控制台后说'$ q.defer不是函数'。

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

这有什么问题?请帮忙 ...!!对于Angular Js来说,如果出现问题,请原谅。

2 个解决方案

#1


21  

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

常见的错误 :)

#2


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.

请参考angular 1.6.1版本文件,因为较旧版本的angular会出现上述错误。

#1


21  

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

常见的错误 :)

#2


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.

请参考angular 1.6.1版本文件,因为较旧版本的angular会出现上述错误。