Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include one partial into other without creating a new scope?

Tags:

angularjs

I've this routes.

// index.html
<div ng-controller="mainCtrl">
    <a href='#/one'>One</a>
    <a href='#/two'>Two</a>
</div>​​​​​​​​​
<div ng-view></div>

And this is how I'm loading the partials into my ng-view.

// app.js
​var App = angular.module('app', []);​​​​​​​
App.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/one', {template: 'partials/one.html', controller: App.oneCtrl});
    $routeProvider.when('/two', {template: 'partials/two.html', controller: App.twoCtrl});
  }]);

When I click the links, it shows me the appropriate markup inside the ng-view. But when I try to include partials/two.html inside partials/one.html using ng-include, it shows it properly but creates a different scope so I'm not able to interact with it.

// partials/two.html - markup
<div ng-controller="twoCtrl">I'm a heading of Two</div>

// partials/one.html - markup
<div ng-controller="oneCtrl">I'm a heading of One</div>
<div ng-include src="'partials/two.html'"></div>

​ How do I resolve this problem? Or Is there any other way to achieve the same result?

like image 450
codef0rmer Avatar asked Sep 12 '12 17:09

codef0rmer


4 Answers

You can write your own include directive that does not create a new scope. For example:

MyDirectives.directive('staticInclude', function($http, $templateCache, $compile) {
    return function(scope, element, attrs) {
        var templatePath = attrs.staticInclude;
        $http.get(templatePath, { cache: $templateCache }).success(function(response) {
            var contents = element.html(response).contents();
            $compile(contents)(scope);
        });
    };
});

You can use this like:

<div static-include="my/file.html"></div>
like image 133
Marius Avatar answered Nov 19 '22 18:11

Marius


The documentation for ngInclude states "This directive creates new scope." so this is by design.

Depending on the type of interaction you are looking for you may want to take a look at this post for one way to share data/functionality between the two controllers via a custom service.

like image 37
Gloopy Avatar answered Nov 19 '22 18:11

Gloopy


So this isn't an answer to this question but i made it here looking for something similar and hopefully this will help others.

This directive will include a partial without creating a new scope. For an example you can create a form in the partial and control that form from the parent controller.

Here is a link to the Repo that i created for it.

good luck :-)

-James Harrington

like image 32
James Harrington Avatar answered Nov 19 '22 17:11

James Harrington


You can actually do this without using a shared service. $scope.$emit(...) can dispatch events to the $rootScope, which can listen for them and rebroadcast to the child scopes.

Demo: http://jsfiddle.net/VxafF/

Reference: http://www.youtube.com/watch?v=1OALSkJGsRw (see the first comment)

like image 4
codef0rmer Avatar answered Nov 19 '22 18:11

codef0rmer