Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 错误:不是函数-角度服务_Javascript_Angularjs - Fatal编程技术网

Javascript 错误:不是函数-角度服务

Javascript 错误:不是函数-角度服务,javascript,angularjs,Javascript,Angularjs,我试图调用服务中定义的函数 var app = angular.module('title', ['flash', 'ngAnimate', 'ngRoute'], function ($interpolateProvider) { $interpolateProvider.startSymbol('[['); $interpolateProvider.endSymbol(']]'); }) .service('getWidgets', fu

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

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不是一个函数


根本原因是什么?

您正在使用服务并在其构造函数上返回一个对象。 服务初始化为
newyourfunction
和工厂名为
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:是一个JSBin,您的代码与我的第一个解决方案一起工作。

试试这种方法

    .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;
        });
    }; 
};
});
对此进行更改:

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

你能把全部代码都贴出来吗?角度。模块(“…”)。服务(…)已添加完整代码。请现在检查。您可以发布控制器的定义(您使用此服务的位置)
angular.module('dss').controller('widgetCtrl'、['$scope'、'$compile'、'$window'、'$location'、'$http'、'globalServices'、'Flash'、'$timeout'、'$sce'、'$routeParams'、'getWidgets',widgetCtrl]);函数widgetCtrl($scope、$compile、$window、$location、$http、globalServices、getWidgets、Flash、$timeout、$sce、$routeParams){var widget=getWidgets.getData();widget.then(函数(结果){$scope.widgets=result;$scope.$apply();});}
谢谢。问题在于订单。您有这个依赖项
'globalServices','Flash','$timeout
,但是控制器函数有:
globalServices,getWidgets,Flash,
同样的问题。我不想退回它的构造或者你去工厂试试?你能添加注入服务的位置吗?@Girish添加了一个JSBin,我的解决方案起作用了。请检查编辑3