我想使用AngularJS将我的服务文件注入我的控制器,所以我可以进行$ http调用,但是当我这样做时,我收到一个错误,它没有定义。
这是我的服务调用在一个单独的文件中,该文件已包含在index.html中。
(function () {
'use strict';
angular.module('App')
.service('MemberService', ['$http', '$q',
function MemberService($http, $q) {
var svc = this;
svc.postMembers = _postMembers;
var deferred = $q.defer();
_postMembers.$http({
method: 'POST',
url: '/api/MemberSearch',
data: data,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function (result) {
deferred.resolve(result)
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
console.log("promise", deferred.promise);
}]);
})();
现在,这是controller.js
(function () {
'use strict';
angular
.module('App')
.controller('MemberController', MemberController);
MemberController.$inject = ['$scope', '$http', '$q', 'MemberService', MemberService];
function MemberController($http, MemberService) {
var vm = this;
vm.search = {}; //a blank object to contain form data
vm.memberSearchForm = null; //reference to the actual form in html
vm.memberResult = []; //a blank object to handle returned data
vm.memberSearch = _memberSearch;
vm.showErrors = false;
function _memberSearch() {
vm.showErrors = true;
console.log("inside _memberSearch");
var promisePost = MemberService.postMembers(vm.search)
console.log("vm.search", vm.search);
}
};
})();
非常感谢任何帮助。