错误:不是函数 - Angular Service

时间:2016-08-30 14:02:00

标签: javascript angularjs

我试图调用服务中定义的函数。

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 个答案:

答案 0 :(得分:2)

改变:

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(); 
        });     
}

答案 1 :(得分:0)

您正在使用服务并在其构造函数上返回一个对象。 服务初始化为 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;
            });
        }
    };
});

编辑3 HERE是一个JSBin,您的代码使用我的第一个解决方案。

答案 2 :(得分:-1)

试试这种方式

EnumWindows