0

Imagine we have an AngularJs app (witten in multiple controllers, services, directive and run method) for multiple routes using $routeProvider. and now we need to use the same application in a single page. meaning that templates of different routes should now be visible in one page at the same time.

I can't use different iframes because then it's hard to access the $scopes of those controllers from the wrapper application.
Is this possible without the use of iframes?

7
  • Use directives? Commented Jan 20, 2017 at 10:07
  • @Ankh How? Can you please explain more? Commented Jan 20, 2017 at 10:07
  • Do not seem any success this way. Commented Jan 20, 2017 at 10:10
  • @Ankh Is it possible to bootstrap an application through a certain path or for a certain controller? Commented Jan 20, 2017 at 10:10
  • templates of different routes should now be visible in one page wrap your pages into directives then you can include them wherever you want. <page-one></page-one><page-two></page-two> Commented Jan 20, 2017 at 10:23

1 Answer 1

1

What you are looking for is ng-include and ng-controller. Using ng-include, You can insert a html into the block containing it and using ng-controller, you can insert a controller for the same block. I would prefer not to use iframes as it is a bad practice and you will not be able to access scope and a lot of features that are native to angular.

EDIT : Since, you are using the run() function you can try the below approach :

Keeping the routeProvider same, you can move the contents of you html template files into script tags on you index.html like so :

<script type="text/ng-template" id="one.tpl.html">
 //Your html template code goes here
</script>
<script type="text/ng-template" id="two.tpl.html">
 //Your html template code goes here
</script>

In you app.js :

$routeProvider.
      when('/', {
        templateUrl: 'one.tpl.html', //points to the content of the script tag in your index.html file
        controller: 'onetplCtrl'
      }).
      when('/edit',{
        templateUrl:'two.tpl.html', //points to the content of the script tag in your index.html file
        controller:'twotplCtrl'
      }).
       otherwise({
        redirectTo: '/'
      });
Sign up to request clarification or add additional context in comments.

3 Comments

This was my first idea, The problem is I'm using run() method in my app which plays a crucial role in the application. and I can't find a solution to get it work with this approach.
Then you can use ng-template to load a particular template to $templateCache which can be used like a normal reference to html file and along with ngRoute pointing a controller to this template, it will work the same as declaring everythng in the routeProvider
I tried your approach and in fact, when I bootstrap the app using ng-app on any element, the run() method is called and if I use ng-controller and ng-include on different elements inside that, everything works fine. they all have access to$rootScope. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.