[英]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.