简体   繁体   中英

Angularjs Reinitializing active controller

Is there a way to reinitialize a controller that is currently active ?

This would help me with transitioning from page to page without adding much additional workaround code.

For example:

<div ng-controller='Blah as ex'>
   {{ex.name}}
</div>

and in the controller Blah's initialization function, the name would be retrieved from a service:

this.name = someService.name;

So for the example above, I'd like to have a button which would reinitialize my Blah controller.

edit: This is only a basic example.

I'm using the browser's 'state' in order to restore 'back' and 'forward' data in my controllers.

There are a few cases so I'm trying to simplify its process (which currently works, but is not as 'pretty')

Would something like this be a possible solution? Using route.reload to reload your page?

myapp.Controller('SampleController', function($location, $route) {

  $scope.navTo = function(url) {
    if ($location.path() === url) {
      $route.reload();
    } else {
      $location.path(url);
    }
  }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM