I'm making an Angularjs/nvd3 dashboard with a number of widgets using json input. I've got a widget to work, but what I'm trying is to reuse an Angular service to return data based on a different url provided per widget.
My code in 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);
}
}
});
});
And the code in the html:
<div id="container" ng-controller="Widget3Controller" ng-app='nvd3myApp'>
<nvd3-multi-bar-chart [.......]></nvd3-multi-bar-chart>
</div>
So what I'd like to do is to have a different url (instead of using fixedUrl) per widget, but I'm unable to have myApp's dataService to take an extra variable.
I'd like to do something like dataService.getData(someUrl), and preferably even use the same controller for multiple widgets based on some html tag.
Thanks for any help.