2017-03-12 11 views
0

私はAngularJSを学び始めており、コントローラからサービスメソッドを呼び出そうとしています。しかし、私がそれを呼び出すとき、関数が定義されていないと言っています。AngularJSでサービス関数が定義されていません

奇妙なことは、コンソールにサービスオブジェクトを印刷すると、その機能がわかります。しかし、私はそれを呼び出すとき、それは未定義と言います。

コントローラ

(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( 'パブリック') .service( 'NewPatientService'、NewPatientService);

function NewPatientService() { 
    var service = this; 
    service.userInfo = null; 
    this.register1 = function() { 

    } 
} 

})(); 

正確なエラーは次のとおりです。

誰かがこの点で何が問題かを指摘できますか?

+0

あなたはregister1を作成しており、regsiter1を呼び出そうとしています。スペルを修正してください。 –

+1

@NikhileshKVありがとう、愚かな間違い。 –

答えて

0

それはする必要があり、またにconsole.log後行NewPatientService.regsiter1();上の問題がある

console.log(NewPatientService.register1()); 

を次のようにそれを削除するか、または変更しようとするためのラインの

console.log(NewPatientService.register1);

ですNewPatientService.register1();

+0

あなたが指しているラインは正常に動作しています。出力は次のようになります。 function(){ } –

+0

問題はまだ解決していますか? – Sajeetharan

+0

はい、削除後も。 –

関連する問題