angularjs - 3 tabs, one controller, different parameters -
i have following need:
a) have template called eventsgraph.html. has 3 tabs: a/b/c b) have controller called eventsgraphctrl.js
the job of eventsgraph.js populate chart objects (using google charts). controller http.get specific url , renders bar graph.
back html: 3 tabs generate graphs. difference a, b , c need call different urls. other code elements of controller same
so needs: a) dont want unique controllers per tab - know how via states - code duplication
b) want 1 graph generation function run within each tab different urls
i thought of using routeparams won't work controller called once template file - won't called switch tabs
i tried converting graph subfunction of controller , calling {{generategraph(url)}} in template runs digest problems looping on , on again due scope data changing
what best way acheive need?
you can move ajax call ng-click of each tab. pass tab id function, , parse switch statement:
<tab-button ng-click="getgraphdata("a")"></tab-button> <tab-button ng-click="getgraphdata("b")"></tab-button> <tab-button ng-click="getgraphdata("c")"></tab-button> in controller:
$scope.data = {}; $scope.getgraphdata = function (tab) { var url; switch (tab) { case 'a': url = 'url tab a' break; case 'b': url = 'url tab a' break; case 'c': url = 'url tab a' } $http.get(url) .success(function(data) { $scope.data[tab] = data }) }
Comments
Post a Comment