[英]Data from controller to service in angularjs
我有一個看起來像這樣的控制器:
class DemandCtrl {
constructor(ChartDataService) {
this.ChartDataService = ChartDataService;
this.dataa = {
from: 'test1',
to: 'test2'
};
}
$onInit() {
getData.call(null, this);
}
}
function getData(DemandCtrl) {
DemandCtrl.ChartDataService.getData().then(result => {
DemandCtrl.result = result.data;
getChart(result.data);
}).catch((e) => {
console.log(e);
});
}
...other methods...
DemandCtrl.$inject = ['ChartDataService'];
export const Demand = {
bindings: {
data: '<'
},
templateUrl: demandPageHtml,
controller: DemandCtrl
};
我想從其中獲取dataa.from
和dataa.to
的內容作為服務中方法的參數的服務。
這是服務的外觀以及我嘗試過的內容:
export default class ChartDataService {
constructor($http, authService) {
this.$http = $http;
this.authService = authService;
}
getData(dataa.from, dataa.to) {
return this.$http.get(`${RTM_API_URL}chartData?interval=FIFTEEN_MINUTES&fromDate=` + dataa.from + `&toDate=`+ dataa.to, config)
.then(result => {
return result;
}).catch(() => {
return Promise.reject('Failed to access chart data ');
});
}
}
ChartDataService.$inject = ['$http', 'authService'];
它說dataa
是未定義的。 有什么主意嗎?
不知道為什么要使用獨立功能getData()。 只需使用this.ChartdataService將代碼放入$ onInit中,並將this.dataa傳遞給對它的getData的調用即可。
class DemandCtrl {
constructor(ChartDataService) {
this.ChartDataService = ChartDataService;
this.dataa = {
from: 'test1',
to: 'test2'
};
}
$onInit() {
this.ChartDataService.getData(this.dataa.from, this.dataa.to).then(result => {
this.result = result.data;
getChart(result.data);
}).catch((e) => {
console.log(e);
})
}
}
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(dataa.from, dataa.to) {
return this.$http.get(`${RTM_API_URL}chartData?interval=FIFTEEN_MINUTES&fromDate=` + dataa.from + `&toDate=`+ dataa.to, config)
.then(result => {
return result;
}).catch(() => {
return Promise.reject('Failed to access chart data ');
});
}
}
ChartDataService.$inject = ['$http', 'authService'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.