我正在用一些使用json輸入的小部件製作Angularjs/nvd3儀表板。我有一個小工具可以工作,但是我想要的是重用一個Angular服務來根據每個小工具提供的不同url返回數據。如何根據變量輸入正確地重用AngularJS服務和控制器?
我的Javascript代碼:
var fixedUrl = "http://static_url/data.json";
var myApp = angular.module("nvd3myApp", ['nvd3ChartDirectives']);
myApp.service('dataService', function($http) {
this.getData = function(callbackFunc) {
return $http.get(fixedUrl);
}
});
myApp.controller('Widget3Controller', function($scope, dataService){
//Get the data
dataService.getData().then(function(dataResponse) {
data = dataResponse.somelogictotransformit();
eval
$scope.exampleData = [
{
"key" : "Widget title",
"color" : "#1f77b4",
"values": data
},
];
$scope.xAxisTickFormatFunction = function(){
return function(d){
return d3.time.format('%H:%M')(new Date(d));
}
}
$scope.yAxisTickFormatFunction = function(){
return function(d){
return d3.format(',d')(d);
}
}
});
});
而在html代碼:
<div id="container" ng-controller="Widget3Controller" ng-app='nvd3myApp'>
<nvd3-multi-bar-chart [.......]></nvd3-multi-bar-chart>
</div>
所以我喜歡做的是有一個不同的URL(而不是使用fixedUrl)每個小部件,但我無法讓myApp的dataService採取額外的變量。
我想要做一些像dataService.getData(someUrl),並最好甚至使用相同的控制器基於一些HTML標籤的多個小部件。
感謝您的任何幫助。
將URL傳遞到getData'dataService.getData(url)''怎麼辦? – dfsq