I assume that you are using AngularJS built-in routing module. If the corresponding controller is associated with a route, it will be initialized whenever the route matches a new location. You cannot avoid this. If you do not want the controller to be created several times, you must define it high in the hierarchy of views. For example, the structure of the main page may be something like this.
<html> ... <body> <div ng-controller="SharedController"> ... <ng-view></ng-view> ... </div> </body> </html>
Here, the SharedController will be created only once, no matter what location users will be moved to. You can move the ng-view beyond the div occupied by the SharedController , although this will prevent sphere inheritance from working, i.e. The areas inside the ng-view will not prototypically inherit from the area entered in the SharedController .
Another option is to use a third-party ui-router library that introduces the concept of nested states. In doing so, you can create a parent state with a controller that is created only once when users get access to various child states.
Buu nguyen
source share