错误:不是功能 - 角度服务

错误:不是功能 - 角度服务

问题描述:

我试图调用服务中定义的函数。错误:不是功能 - 角度服务

var app = angular.module('title', ['flash', 'ngAnimate', 'ngRoute'], 
    function ($interpolateProvider) { 

     $interpolateProvider.startSymbol('[['); 
     $interpolateProvider.endSymbol(']]'); 
    }) 

.service('getWidgets', function (globalServices, $http) { 

    var getData = function() { 

     var getWidgetUrl = globalServices.baseUrl + "admin/widget/list-text-widget"; 
     return $http({method:"GET", url:getWidgetUrl}) 
      .then(function(result){ 

       return result.data; 
      }); 
    }; 

    return { getData: getData }; 
}); 

呼唤节

var widget = getWidgets.getData() 
    .then(function (result) { 

     $scope.widgets = result; 
     $scope.$apply(); 
    }); 

但它返回一个错误getWidgets.getData is not a function

根本原因是什么?

+3

可以张贴整个代码? angular.module('...')。service(....) – hpfs

+0

完整代码已添加。请立即检查。 – Girish

+0

您可以发布您的控制器定义(您使用此服务的地方) – gianlucatursi

更改与此:

angular.module('dss') 
    .controller('widgetCtrl', 
    ['$scope', '$compile', '$window', '$location', '$http', 'globalServices', 'getWidgets', 'Flash', '$timeout', '$sce', '$routeParams', widgetCtrl]); 

    function widgetCtrl($scope, $compile, $window, $location, $http, globalServices, getWidgets, Flash, $timeout, $sce, $routeParams) { 

    var widget = getWidgets.getData(); 
    widget.then(
     function (result) { 
      $scope.widgets = result; $scope.$apply(); 
     });  
} 

编辑:如果你想要一个建议,请使用此语法:

widgetCtrl.$inject = ['$scope', '$compile', '$window', '$location', '$http', 'globalServices', 'getWidgets', 'Flash', '$timeout', '$sce', '$routeParams']; 

angular.module('dss').controller('widgetCtrl', widgetCtrl); 

function widgetCtrl($scope, $compile, $window, $location, $http, globalServices, getWidgets, Flash, $timeout, $sce, $routeParams) { 

    var widget = getWidgets.getData(); 
    widget.then( 
     function (result) { 
      $scope.widgets = result; $scope.$apply(); 
     });  
} 
+0

谢谢你。 – Girish

您正在使用服务并在其构造函数上返回一个对象。 服务初始化为 new yourFunction和工厂为yourFunction()

将它从服务切换到工厂,它将工作。

编辑:如果你想继续使用服务,试试这个。 注意,我改变了服务的名称

function GetWidgetsService($http, globalServices){ 
    this._$http = $http; 
    this._globalServices = globalServices; 
} 

GetWidgetsService.prototype.getData = function() { 
    var getWidgetUrl = this._globalServices.baseUrl + "admin/widget/list-text-widget"; 
    // Angular $http() and then() both return promises themselves 
    return this._$http({method:"GET", url:getWidgetUrl}).then(function(result){ 

     // What we return here is the data that will be accessible 
     // to us after the promise resolves 
     return result.data; 
    }); 
}; 

angular.module('yourModule').service('getWidgetsService', GetWidgetsService); 

编辑2:为了完整起见,这里是你的固定工厂

angular.module('yourModule').factory('getWidgetsFactory', function ($http, globalServices) { 
    return { 
     getData: function() { 
      var getWidgetUrl = globalServices.baseUrl + 'admin/widget/list-text-widget'; 
      // Angular $http() and then() both return promises themselves 
      return $http({method: 'GET', url: getWidgetUrl}).then(function (result) { 

       // What we return here is the data that will be accessible 
       // to us after the promise resolves 
       return result.data; 
      }); 
     } 
    }; 
}); 

编辑3HERE是JSBin与你的代码工作我的第一个方案

+0

让我试试这个。 – Girish

+0

@Girish更新了答案 –

+0

同样的问题。我并非试图返回它的构造函数 – Girish

尝试这种方式

.service('getWidgets', function (globalServices, $http) { 
    return { getData: function() { 
     var getWidgetUrl = globalServices.baseUrl + "admin/widget/list-text-widget"; 
     // Angular $http() and then() both return promises themselves 
     return $http({method:"GET", url:getWidgetUrl}).then(function(result){ 

      // What we return here is the data that will be accessible 
      // to us after the promise resolves 
      return result.data; 
     }); 
    }; 
}; 
});