2017-10-24 6 views
1

へのサービスからメソッドを呼び出します。私はこのサービスの構造を有するコントローラ

export default class ChartDataService { 
    constructor($http, authService) { 
     this.$http = $http; 
     this.authService = authService; 
    } 
    getData(dataa) {  

     //do something 

} 

ChartDataService.$inject = ['$http', 'authService']; 

とコントローラ構造:

class DemandCtrl { 
    constructor(ChartDataService) { 
     this.ChartDataService = ChartDataService; 
     this.dataa = { 
      from: '10/01/2017', 
      to: '10/03/2017' 
     }; 
    } 

    $onInit() { 
     getData.call(null, this);  
    } 

    update() { 
     console.log('show something'); 
     DemandCtrl.ChartDataService.getData(this.dataa);   
    } 
} 
... 
DemandCtrl.$inject = ['ChartDataService']; 

export const Demand = { 
    bindings: { 
     data: '<' 
    }, 
    templateUrl: demandPageHtml, 
    controller: DemandCtrl 
}; 

を私がサービスからupdate()getData()関数を呼び出したいです。どのように試しても、getData()ChartDataService.getData(this.dataa)またはDemandCtrl.ChartDataService.getData(this.dataa)と同じエラーが表示されます。は未定義です。

どうすればこの問題を解決できますか?

+0

あなたは、どこかでサービスとして登録する(https://docs.angularjs.org/guide/services#registration-services)? – tiagodws

答えて

0

答えは、$onInit()のように.call()を使用しています。この特定のケースで

update()の内容でなければならない:[ドキュメントに記載された]として

update() { 
     getData.call(null, this);  
    } 
関連する問題