javascript - How to reuse a controller in angularjs with different variables based on route -


i have requirement load web service. liked angular way of displaying, sorting , filtering tables, , built worked great. i'm trying expand this: based on link selected, i'd display different data, same web service, in same way. i'm trying not duplicate code i'm trying reuse controller , i'm confused. means want to

  • display different partial / template url.
  • load different url parameters.
  • map few things after json has been returned.

what best way of tackling i've far considered more changes in config? started routing variable (when('/change-requests/:businessunit'), , use load different values. but, how change template url? on other hand, if add route , load different url template (reusing controller), how load different url in ajax call? i'm not familiar angular, maybe i'm barking wrong tree...

here relevant parts of original code. there's more none of has change. i've put inline comments you'll find bits i'd change, depending on links selected.

html:

<div ng-view></div>

router:

  $routeprovider.when('/change-requests/business-solutions', {                     //this template different                     templateurl: 'js/modules/changerequests/partials/business-solutions.html',                     controller: changerequestctrl                 }) 

(parts of the) controller:

        function changerequestctrl($scope, $location) {                  $scope.loaded = false;                 $.ajax({                     //this url different                     url: url,                     success: function(json) {                         $scope.$apply(function() {                             $scope.changes = json.map(function(row) {                                 //this array map different                                 return row;                             });                             $scope.loaded = true;                         });                     },                     error: function() {                         $scope.error = true;                     }                 });         } 

if i'm not making sense, i'll try clarify.

if want use reuse controller name should using resolve property routes , use resolve property in controller

 $routeprovider.when('/change-requests/business-solutions', {                     //this template different                     templateurl: 'js/modules/changerequests/partials/business-solutions.html',                     controller: changerequestctrl, resolve: {             resolvedprop: ['$route', '$q', function ($route, $q) {                var apiobject = {url: 'abc.com' };                                      return apiobject                           }],         }                 })        function changerequestctrl($scope, $location,resolvedprop) {        url = resolvedprop.url ;        } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -