daxu
daxu

Reputation: 4074

Can I force controller to reload some data when people hit back button even on the same route in angular?

In my angular app, this is my route pattern:

 $routeProvider
    .when('/DocumentUploader/folder/:folderid?', {
        templateUrl: '/FileManagers/views/items.html',
        controller: 'ItemsCtrl',
        controllerAs: 'items'
    })
    .otherwise({
        redirectTo: '/DocumentUploader/folder'
    });

When the page loads, the default route is: http://localhost:5460/Workbenchv2.cshtml#/DocumentUploader/folder

So this will go to the root folder.

Then user can click a folder name, and the route changes to http://localhost:5460/Workbenchv2.cshtml#/DocumentUploader/folder/92E39760-156D-4C62-A295-E4D1C20706CD (internal ID for the folder)

Now if user hit the back button, angular will not reload the controller (as it is the same route), so I can't refresh my data.

Is it possible to force angularjs to reload the controller, even if the route is the same?

Upvotes: 0

Views: 327

Answers (1)

Nayan
Nayan

Reputation: 1605

I've created a Plunker here: Plunker Link

In my case when user click the back button, the controller is reloaded. Here is the controller that I'm using:

myApp.controller('ItemsCtrl', ['$scope', '$routeParams', 
  function($scope, $routeParams){
    var folderId = $routeParams.folderid;

    if(!folderId){
      $scope.msg = 'You are in root folder';
    } else {
      $scope.msg = 'You are in folderId=' + folderId;
    }
  }
]);

Upvotes: 1

Related Questions