2017-10-23 15 views
0

でのサービスに:私はパラメータとしての私のサービスにdataaからデータを取得したい送信パラメータは、私は、次のコントローラを持ってangularjs

class DemandCtrl { 
    constructor(ChartDataService) { 
     this.ChartDataService = ChartDataService; 
     this.dataa = { 
      from: 'test1', 
      to: 'test2' 
     }; 
    } 

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

} 


function getData(DemandCtrl) { 
    debugger; 
    DemandCtrl.ChartDataService.getData().then(result => { 
     DemandCtrl.result = result.data; 
     getChart(result.data); 
    }).catch((e) => { 
     console.log(e); 
    }); 
} 


    ... other methods/functions... 


DemandCtrl.$inject = ['ChartDataService']; 

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

サービスは、次のようになります。私が試した何

export default class ChartDataService { 
    constructor($http, authService) { 
     this.$http = $http; 
     this.authService = authService; 
    } 
    getData() { 
       return this.$http.get(`chartData?&fromDate=` + dataa.from + `&toDate=` + dataa.to) 
.then(result => { 
      return result; 
     }).catch(() => { 
      return Promise.reject('Failed to access chart data '); 
     }); 
    } 
} 

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

getData()にパラメータとしてdataaを与えることですが、それはそれは未定義であると言います。おそらく私はコントローラから正しく送信していませんが、どうすればいいのか分かりません。

答えて

0

ChartDataServiceサービスでは、約束を解く代わりにhttpリクエストを返すだけです。

export default class ChartDataService { 
    constructor($http, authService) { 
     this.$http = $http; 
     this.authService = authService; 
    } 
    getData(dataa) { 
     return this.$http.get(`chartData?&fromDate=` + dataa.from + `&toDate=` + dataa.to) 
    } 
} 

ここでこのようなパラメータを渡します。

DemandCtrl.ChartDataService.getData(this.dataa).then(result => { 
     DemandCtrl.result = result.data; 
     getChart(result.data); 
    }).catch((e) => { 
     console.log(e); 
    }); 
+0

私は 'DemandCtrl.result = result.dataにエラーが発生しました;': 'standalone.js:16 TypeError例外:プロパティを読み取ることができませんヌル の 'DATAA' のgetDataで(demand.js a5a5:24?) standalone.jsで 。:DemandCtrl $のOnInit(17?demand.js a5a5):でnodeLinkFnで (standalone.js:16) standalone.jsで:16 で:forEachの(16 standalone.js)で16 processQueue(standalone.js:16)standalone.jsで :スコープで16 $ダイジェスト(standalone.js:16)。スコープで $適用されます(standalone.js:16)。済(standalone.jsで :16 ) at completeRequest(standalone.js:16) at XMLHttpRequest.requestLoaded(standalone.js:16) ' –

関連する問題