gpt4 book ai didi

javascript - 错误: Not a function - Angular Service

转载 作者:行者123 更新时间:2023-11-27 22:34:41 25 4
gpt4 key购买 nike

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

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

根本原因是什么?

最佳答案

更改如下:

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

关于javascript - 错误: Not a function - Angular Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com