2

I have an angular 7 application using a angular ui-router@2.0.2 where I need to switch between views while maintaining the state of the views all the while.

I dont want ngOnInit to be called upon each route change, I'd rather want the views (even better - a specific view) to just switch between show/hide, without initializing/destroying the view components each time.

I've searched the ui-router documentation but couldn't find anything helpful.

I can probably workaround this problem by saving the state of each view in a service and loading it again but I'd rather avoid that.

Thanks in advance for any help!

  • 1
    Why not using CanActivate ? – Abdulrahman Falyoun May 26 '19 at 07:41
  • 1
    I don't need to decide whether I should switch the view or not, I need a way to switch to the destination view without rendering the destination component. A simple hide/show of a css property would suffice. – Programmer1 May 26 '19 at 07:54
  • Have you looked into state management solutions such as a shared service (using RxJs Subjects) or NGRX (though it might be heavyweight for what you're doing). These can be used to keep viewstate between navigation. Relevant reading: https://blog.angular-university.io/how-to-build-angular2-apps-using-rxjs-observable-data-services-pitfalls-to-avoid/ – Zailef May 26 '19 at 08:58
  • Thank you @Zailef. I wrote I can probably workaround the problem using state management but I prefer to avoid that. I hope there's a way to just show/hide a certain route view. – Programmer1 May 26 '19 at 09:45

0 Answers0