shay.k shay.k - 20 days ago 7
AngularJS Question

Can i use an angular controller in a page based on its controller ID?

I am using the same controller in several HTML pages in my project.
My problem is that each page links to the other and instead of them all using the same exact controller they duplicate it each time the next page is opened and each page is using its own controller, which means, all the data saved in the previous controller will not apply on the next page with the new duplicated controller.

I recently acknowledged that each controller has its own ID (

$scope.$id
), so is there an option for me to request a specific controller to be used on the HTML page based on its controller ID?

Or is there an alternate option to store the variables of the controller in another place where i could use them without being concerned that they will reset on each page?

Thanks, and i hope i supplied enough information for you to understand my issue.

Answer
<div ng-controller="myCtrl">
            <div ng-switch on="condition">
                <div ng-switch-when="condition_output_1">
                   <div ng-include="'html_page_1_url.html'"></div>
                </div>
                <div ng-switch-when="condition_output_2">
                     <div ng-include="'html_page_1_url_2.html'"></div>
                </div>
            </div>
    </div>

Now, Swap html_page_1_url.html and html_page_2_url.html or any html

myCtrl this will remian same for all.


To transfer data between controllers.. You can use

  • $rootscope
  • services
  • localstorage
Comments