Dania Dania - 7 months ago 43
AngularJS Question

prevent re initialization of controller on $location.path()

I am working on a application where I have to insert a back navigation link to main page from details page. Controller for both views are different. I am using $location.path('/') to navigate back to main page. Problem is, my controller for main page is re initialized when I navigate back by clicking on this link, which is not the behaviour required. Is there a way to prevent re initialisation of controller again when routing back to same link.


As I had more than one apps in my angular project, It was difficult to implement answer posted above as I have my ng-view much above in hierarchy that decides which app to be displayed to user. So, the solution is to store data inside a service. Service do not re instantiate when navigate within an app, so the data remains intact.

Inside controller,

// Check if data is present in service

if (service.dataModel && service.dataModel.data) {
    // insert data in scope variables here

else {
   // fetch data from server and add data model to service.