我開始學習AngularJS,並試圖從控制器調用服務方法。但是當我調用它時,它說這個函數是未定義的。AngulajJS中未定義服務函數
奇怪的是當我在控制檯中打印服務對象時,我可以看到該函數。然而,當我嘗試打電話時,它說未定義。
控制器
(function() {
angular.module('public')
.controller('NewPatientController', NewPatientController);
NewPatientController.$inject = ['NewPatientService'];
function NewPatientController(NewPatientService) {
var $ctrl = this;
console.log(NewPatientService.register1);
$ctrl.mess = 'hhtg';
$ctrl.success = null;
NewPatientService.regsiter1();
$ctrl.submit = function() {
console.log('Ctrl');
NewPatientService.regsiter1();
}
}
})();
服務
(函數(){ angular.module( '公共') 。服務( 'NewPatientService',NewPatientService);
function NewPatientService() {
var service = this;
service.userInfo = null;
this.register1 = function() {
}
}
})();
我得到的確切錯誤是:
NewPatientService.regsiter1 is not a function
有人可以指出什麼可能是這個問題。
您已經創建了register1並正試圖調用regsiter1。請修正拼寫。 –
@NikhileshKV謝謝,愚蠢的錯誤。 –