Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:10:02+00:00 2026-06-17T15:10:02+00:00

I want to have element where i can have 2 views using their own

  • 0

I want to have element where i can have 2 views using their own controller but only one at a time.
I can’t use a ng-view and use the routeProvider because in the future I need to include more ng-includes that need to change their content depending on the possible actions.

I created a fiddle http://jsfiddle.net/EvHyT/29/.

So I used a ng-include and then I set the src for it from a main controller. At that point I want to use controller 1 or controller 2.

function MainCtrl($rootScope, $scope, navService){
$scope.template = {};

$scope.loadCtrl1=function(param){
    navService.loadCtrl1(param);
}

$scope.loadCtrl2=function(param){
    navService.loadCtrl2(param);
}

$rootScope.$on('loadCtrl1', function(e, args){
    $scope.template = {'url': 'temp1'};
});

$rootScope.$on('loadCtrl2', function(e, args){
     $scope.template = {'url': 'temp2'};       
});
}

I use a service for communication because i want to move the load controller functions in a child controller.

var myApp = angular.module('myApp',[]);


myApp.factory('navService', function($rootScope) {
return {
     loadCtrl1:function(param){
        $rootScope.$broadcast('loadCtrl1', {'id':param});   
    },

    loadCtrl2:function(param){
        $rootScope.$broadcast('loadCtrl2', {'id':param});
    }
};
});

I know this solution is bad because the controllers are not yet created when a different template is inserted so my event listener will not fire. Also can I destroy the previous instances of the controller because switching between the two controllers makes my event fire multiple times.

function Child1Ctrl($scope, $rootScope){
 $rootScope.$on('loadCtrl1', function(e, args){
   alert(args.id);
});
}

function Child2Ctrl($scope, $rootScope){

$rootScope.$on('loadCtrl2', function(e, args){
      alert(args.id);
});
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:10:03+00:00Added an answer on June 17, 2026 at 3:10 pm

    You don’t need to broadcast (and shouldn’t be broadcasting) to make this happen.
    In my experience, if you’re broadcasting on the rootScope, you’re probably working too hard.

    A simpler way of loading the template is very similar to what you’re doing:

    my.NavService = function() {
      this.template = 'index.html';
      this.param = null;
    };
    my.NavService.prototype.setTemplate(t, p) {
      this.template = t;
      this.param = p;
    };
    
    my.ctrl = function($scope, nav) {
       $scope.nav = nav;
       $scope.load = function(t, p) {
         nav.setTemplate(t, p);
       };
    };
    
    my.ctrl1 = function($scope, nav) {
      $scope.param = nav.param;
    };
    
    my.ctrl2 = function($scope, nav) {
      $scope.param = nav.param;
    };
    
    module.
      service('nav', my.NavService).
      controller('mainCtrl', my.ctrl).
      controller('ctrl1', my.ctrl1).
      controller('ctrl2', my.ctrl2);
    
    <script type="text/ng-template" id="temp1.html">
      <div ng-controller="ctrl1">Foo {{param}}.</div>
    </script>
    <script type="text/ng-template" id="temp2.html">
      <div ng-controller="ctrl2">Bar {{param}}.</div>
    </script>
    
    <div ng-controller="mainCtrl">
      <a ng-click="load('temp1.html', 16)">Load 1</a>
      <a ng-click="load('temp2.html', 32)">Load 2</a>
      <div ng-include src="nav.template"></div>
    </div>
    

    A setup like this would work much better for you.

    Alternatively, you should look into selectively showing elements with ng-switch. Unlike ng-show/hide, ng-switch does not simply add “display:none” to the CSS. It removes it from the DOM.

    Some notes:

    • The above example may not be a working example, it’s a demonstration of the idea.
      A working example can be seen here: http://jsbin.com/ofakes/1 It modifies your original code.
    • JSFiddle had some issues with loading the include from the on page script
      tag.
    • JSBin was a little better.
    • I didn’t really get it to work as expected until I broke out the templates
      into their own files.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a container element that you can drag objects around in. I want
I have an image that I want to move. I can move the element
we want to have the second biggest element. We first use ANY to exclude
I have converted svg to image using Raphael.js , canvg and jquery. But want
I am using play 1.2.5. In one of my views I have a datatable
I have a couple of properties in my view model that are display-only but
I have one background I want constant to all views; it shouldn't animate out
In my ASP.net MVC App (using Razor views) I have a ProductDetails view. This
I have a several views that I can drag around, rotate, scale. I want
I'm using Backbone.js have a segmented control-type UI element for each model's view. They

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.