每當我運行由angularjs 1.4.3實現的應用程序時。TypeError:通過角度調用API時服務不是函數js
BannerService.js
app.service('BannerService',function ($http) {
this.save = function saveBanner(banner) {
return $http({
method: 'POST',
url: 'http://localhost:8081/api/banners'
});
}
});
BannerController.js
app.controller('BannerAddCtrl', ['$scope','$log','BannerService',
function ($scope,$log, BannerService) {
$scope.save = function() {
BannerService.saveBanner(myBanner)
.success(function (result) {
$log.debug('RESULT', result);
}, function (reason) {
$log.debug('REASON', reason);
});
}
}]);
和index.html
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-block" ng-click="save()">Save Banner</button>
</div>
它拋出如下異常:
TypeError: BannerService.saveBanner is not a function
at ChildScope.$scope.save (bannerControllers.js:64)
at fn (eval at compile (angular.js:13145), <anonymous>:4:203)
at callback (angular.js:23298)
at ChildScope.$eval (angular.js:15846)
at ChildScope.$apply (angular.js:15945)
at HTMLButtonElement.<anonymous> (angular.js:23303)
at HTMLButtonElement.dispatch (jquery.js:4435)
at HTMLButtonElement.elemData.handle (jquery.js:4121)
有人可以幫助我,爲什麼我得到的錯誤。非常感謝你的時間。
謝謝它對我有用。 –
很高興我能幫到你。獲得標記爲正確回答將是很好,但。 :) – Aer0