我有一个看起来像这样的控制器:
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
未定义。有什么想法是什么好方法呢?
答案 0 :(得分:0)
不确定为什么你有独立函数getData()。只需将该代码放在$ onInit中使用this.ChartdataService并将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'];