Angularjs 在返回承诺时,它给我一个未知的提供程序错误
这是工厂Angularjs 在返回承诺时,它给我一个未知的提供程序错误,angularjs,Angularjs,这是工厂 latModule.factory('latSvc', [ "$http", "$scope", "$q", function($http, $scope, $q) { console.log("Enter latUserReportDateSvc"); return { getPromiseForUserReportDate: function () { $scope.userI
latModule.factory('latSvc',
[
"$http", "$scope", "$q",
function($http, $scope, $q) {
console.log("Enter latUserReportDateSvc");
return {
getPromiseForUserReportDate: function () {
$scope.userId = "bpx3364";
var deferred = $q.defer();
$http.get('/api/UserReportStatusApi', { 'userId': $scope.userId }).then(function(reponse) {
deferred.resolve(reponse);
},
function(error) {
deferred.reject(error);
});
return deferred.promise;
},
getPromiseForLocation: function () {
$scope.userId = "bpx3364";
var deferred = $q.defer();
$http.get('api/UserAccountApi/', { 'userId': $scope.userId }).then(function (reponse) {
deferred.resolve(reponse);
},
function (error) {
deferred.reject(error);
});
return deferred.promise;
},
getPromiseForErrorSummary: function (userInfoVm) {
console.log("latErrorSummarySvc getErrorCounts, userInfo: ", userInfoVm);
$scope.userId = "bpx3364";
$scope.serviceTypeCode = 4;
var deferred = $q.defer();
$http.get('/api/UserReportStatusApi', { 'userId': $scope.userId, 'serviceTypeCode': $scope.serviceTypeCode }).then(function (reponse) {
deferred.resolve(reponse);
},
function (error) {
deferred.reject(error);
});
return deferred.promise;
}
};
}
]);
这是控制器
latModule.controller("dashboardController",
["$scope","latSvc",
function ($scope,latSvc) {
console.log("enter dashboard controller");
console.log("scope: ", $scope);
console.log("homeUserInfo: ", $scope.homeLatUserInfo);
var dashboardUserReportDate = function() {
latSvc.getUserReportDateInfo().then(
function(response) {
$scope.dashboardUserReportDateData = response;
}, function(error) {}
);
};
dashboardUserReportDate();
var dashboardErrorCounts = function() {
latSvc.getPromiseForErrorSummary($scope.homeLatUserInfo).then(
function(response) {
$scope.dashboardErrorCountsData = response;
},
function (error) { }
);
};
dashboardErrorCounts();
var dashboardAtmCount = function() {
latSvc.getPromiseForLocation().then(
function(response) {
$scope.dashboardAtmCountData = response;
}, function(error) {}
);
};
dashboardAtmCount();
}]);
运行此代码后,我在尝试实现此承诺概念时遇到未知的提供程序错误。因为当我在未解析承诺的情况下通过服务调用时,url被多次命中。您无法使用/向服务中注入$scope。 但就我所理解的代码而言,您应该可以使用局部变量作为http请求查询参数
Vineet是对的-您应该直接返回$http.get,这已经是一个承诺。您能准确地告诉我您得到的错误吗?您应该编写代码从facory或service返回承诺。它应该是从工厂返回的$http.get,并在您的控制器中使用返回的承诺。您可以展示您的解决方案的示例吗?