AngularJS - 从控制器访问工厂服务
AngularJS - access factory service from the controller
我正在尝试访问 controller
内的 factory
服务以获得正确的数据。
相关的控制器代码如下:
myApp.controller('RegistrationController', ['$scope','$routeParams','$rootScope','$location','$filter','$mdDialog','checkAttendee', function($scope, $routeParams, $rootScope, $location, $filter, $mdDialog,checkAttendee){
...
$scope.addAttendee = function(ev) {
$mdDialog.show({
controller: AddDialogCntrl,
templateUrl: 'views/regForm.tmpl.html',
parent: angular.element(document.body),
targetEvent: ev,
clickOutsideToClose:true,
controllerAs: 'ctrl',
fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
locals: {parent: $scope}
})
.then(
function(response){
if(angular.isDefined(response)){
attendees.push(response);
checkAttendee.getAttendeeInfo(response);
}
},
function(){
//no changes
}
)
.catch(
function(error) {
console.log('Error: ' + error);
}
)
};
和factory
服务代码
myApp.factory('checkAttendee', ['$http', function($http) {
this.getAttendeeInfo = function(req) {
return $http.get("/check/attendee/", {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
.then(function(response) {
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
console.log('Data: ' + data);
console.log('Status: ' + status);
return data;
})
.catch(function(response) {
console.log('something worng');
});
}
}]);
但是当存在 return 值时,该组合会给我一个错误 Provider 'checkAttendee' must return a value from $get factory method.
。
有什么想法吗?
选项 1
当我们与工厂合作时,结构应该是:
myApp.factory('checkAttendee', ['$http', function($http) {
var factory = {
getAttendeeInfo : function () {
return $http.get(/**/).then(function(response) {
// ..
return data;
}
}
}
return factory;
}]);
选项 2
您可以将 factory
更改为 service
并且一切正常。 A.e.:
myApp.service('checkAttendee', ['$http', function($http) {
this.getAttendeeInfo = function(req) {
return $http.get("/check/attendee/", {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
.then(function(response) {
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
console.log('Data: ' + data);
console.log('Status: ' + status);
return data;
})
.catch(function(response) {
console.log('something worng');
throw response;
});
}
}]);
请记住,service
扩展了 factory
我正在尝试访问 controller
内的 factory
服务以获得正确的数据。
相关的控制器代码如下:
myApp.controller('RegistrationController', ['$scope','$routeParams','$rootScope','$location','$filter','$mdDialog','checkAttendee', function($scope, $routeParams, $rootScope, $location, $filter, $mdDialog,checkAttendee){
...
$scope.addAttendee = function(ev) {
$mdDialog.show({
controller: AddDialogCntrl,
templateUrl: 'views/regForm.tmpl.html',
parent: angular.element(document.body),
targetEvent: ev,
clickOutsideToClose:true,
controllerAs: 'ctrl',
fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
locals: {parent: $scope}
})
.then(
function(response){
if(angular.isDefined(response)){
attendees.push(response);
checkAttendee.getAttendeeInfo(response);
}
},
function(){
//no changes
}
)
.catch(
function(error) {
console.log('Error: ' + error);
}
)
};
和factory
服务代码
myApp.factory('checkAttendee', ['$http', function($http) {
this.getAttendeeInfo = function(req) {
return $http.get("/check/attendee/", {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
.then(function(response) {
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
console.log('Data: ' + data);
console.log('Status: ' + status);
return data;
})
.catch(function(response) {
console.log('something worng');
});
}
}]);
但是当存在 return 值时,该组合会给我一个错误 Provider 'checkAttendee' must return a value from $get factory method.
。
有什么想法吗?
选项 1
当我们与工厂合作时,结构应该是:
myApp.factory('checkAttendee', ['$http', function($http) {
var factory = {
getAttendeeInfo : function () {
return $http.get(/**/).then(function(response) {
// ..
return data;
}
}
}
return factory;
}]);
选项 2
您可以将 factory
更改为 service
并且一切正常。 A.e.:
myApp.service('checkAttendee', ['$http', function($http) {
this.getAttendeeInfo = function(req) {
return $http.get("/check/attendee/", {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
.then(function(response) {
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
console.log('Data: ' + data);
console.log('Status: ' + status);
return data;
})
.catch(function(response) {
console.log('something worng');
throw response;
});
}
}]);
请记住,service
扩展了 factory