In my search for a way to create a controller only once, I have not come across any satisfactory solutions.
I have discovered some alternative approaches:
- Creating a controller list in $rootScope and adding newly created controllers to it
- Developing a masterController where all created controllers are saved in its scope collection
Therefore, when my template is loaded, I need to utilize a specialized service to manage the controllers. The service must perform checks to determine if a controller has already been created - if so, load the existing one instead of creating a new one; otherwise, add the newly created controller to the collection.
The approach may seem straightforward but appears cumbersome when working with Angular. Maybe I am lacking expertise in Angular, which is why I have struggled to find a better solution.
Can you offer any suggestions for improvement or an alternative solution!? Maybe there's something already available in the base library that I'm unaware of...
Further clarification:
ng-app=singleApplication
|-ng-view
The SingleApplication utilizes ng-route to handle templates within ng-view. Each unique template has its own distinct controller. Every time a different view than the current one is applied, a new controller is instantiated. However, I want to avoid unnecessary server requests for data if a template has already been loaded before. I need to display the template with previous data, tables, and controls without re-creating the controller each time. All data is stored in the controller-scope (this), where all keys are unique, but I am uncertain whether to store the data in $scope or $rootScope.