ng-include passing parameters to templates

33,331 views
Skip to first unread message

martymartino

unread,
Aug 2, 2012, 11:04:58 AM8/2/12
to ang...@googlegroups.com
Hi

Im trying to re-use a template on a page, with a slight variation (in this case, address input for delivery and address input for billing)

I have something like:
  <ng-include src="'partials/addressform.html'" onload="type='billing';"></ng-include>
  <ng-include src="'partials/addressform.html'" onload="type='delivery';"></ng-include>

However since the type is getting bound to the parent scope the second include wipes out the first so on the page they both appear as type delivery.

is there anyway to apply this onload expression to the child scope - i.e. the scope of the included template?

Any suggestions much appreciated

thanks
Martin

Pawel Kozlowski

unread,
Aug 2, 2012, 3:15:27 PM8/2/12
to ang...@googlegroups.com
Hmm, effectively, I would also expect onLoad to be executed in a
newly created scope...
Here is the jsFiddle illustrating the pb:
http://jsfiddle.net/pkozlowski_opensource/ecyDk/1/

I think we could open a bug report / feature request for this as for
me it would make sense to be able to specify an expression to be
evaluated in a child scope.

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Brice Burgess

unread,
Aug 2, 2012, 3:59:07 PM8/2/12
to ang...@googlegroups.com
Looking at the code for ngInclude it's clear that onLoad is executed on the directive's scope (the PARENT scope of the INCLUDED content/scope). Further, an "includeContentLoaded" is emitted from the child scope (broadcasted upwards, so that it will not reach the child scope). This event could theoretically be used to pass the `type=billing` assignment.. although being able to flag the onLoad expression to be evaluated against the include's scope would be helpful here. E.g. something like <ng-include src="'partials/addressform.html'" childOnload="type='billing';">.

Below is a relevant reference to the current (AngularJS 1.0.1) implementation of ngInclude

return {
restrict: 'ECA', ...
compile: function(element, attr) {
  var srcExp = attr.ngInclude || attr.src,
      onloadExp = attr.onload || '', ...
      autoScrollExp = attr.autoscroll;

  return function(scope, element) {...
    scope.$watch(srcExp, function(src) {
      if (src) {
        $http.get(src, {cache: $templateCache}).success(function(response) { ...

          childScope.$emit('$includeContentLoaded');
          scope.$eval(onloadExp);
        })....
      }
    });
  };
}
};

would submitting a patch to include childOnload behavior make sense && if so; what would you suggest the attribute be named?
> angular+unsubscribe@googlegroups.com.

Pawel Kozlowski

unread,
Aug 2, 2012, 4:03:56 PM8/2/12
to ang...@googlegroups.com
Hi Brice!

On Thu, Aug 2, 2012 at 9:59 PM, Brice Burgess <bric...@gmail.com> wrote:

> would submitting a patch to include childOnload behavior make sense && if
> so; what would you suggest the attribute be named?

If you ask me it would definitively make sense, this would enable
Tiles-like (http://tiles.apache.org/) way of composing includes, this
could be super-cool! As for naming - don't know: onLoaded? afterLoad?
onInclude?

Cheers,
Pawel

Brice Burgess

unread,
Aug 2, 2012, 5:04:09 PM8/2/12
to ang...@googlegroups.com
Pawel,

I liked the name of onInclude, because it nicely coincides with onLoad. I've made a patch and pull request which can be seen @
  https://github.com/angular/angular.js/pull/1227

A demo of this behavior using an Angular build with the above patch is available @
  http://jsfiddle.net/briceburg/NMmyY/1/

Hopefully the Angular gods will be able to get through issues+pull requests as per the blog post ! ;)

~ Brice

martymartino

unread,
Aug 3, 2012, 6:57:03 AM8/3/12
to ang...@googlegroups.com
Hi guys!

thanks so much for all this great info/help.

Brice, I hope your patch gets taken up, it's a great addition for just a few lines of code! I'm definitely going to use your patch!

As for naming; onInclude is nice, but I think that something like onLoadChild would be more informative (although uglier).

thanks again
Martin

Denis Pshenov

unread,
Dec 17, 2013, 11:54:08 AM12/17/13
to ang...@googlegroups.com
Take a look here for a simple solution: http://stackoverflow.com/a/20639139/220086

In short:

<div ng-repeat="name in ['A']" ng-include="'partial.html'"></div>
<div ng-repeat="name in ['B']" ng-include="'partial.html'"></div>

<script type="text/ng-template" id="partial.html"> <div>{{ name }}</div> <script>

Michael Hopper

unread,
Jan 15, 2014, 7:36:52 PM1/15/14
to ang...@googlegroups.com
Why not just use ng-init?  For example:

  <div ng-init="name = 'Mike'" ng-include="'partial.html'"></div>

Andrew Pashkin

unread,
Oct 1, 2014, 5:50:31 AM10/1/14
to ang...@googlegroups.com
+1 - seems like ngInit fits well for that purpose (and works too).

Atluri Ajith Kumar

unread,
Oct 29, 2014, 2:10:33 AM10/29/14
to ang...@googlegroups.com
ng-init doesn't work for me..

Sander Elias

unread,
Oct 29, 2014, 2:23:37 AM10/29/14
to ang...@googlegroups.com

Hi Atluri,

Sure it does: http://plnkr.co/edit/SKDCES?p=preview
In your version, both templates shared a single scope/controller. In a single scope, there can only be one scope.name
so If you change the name then, it will change everywhere where you are using it.

Regards
Sander

Atluri Ajith Kumar

unread,
Oct 29, 2014, 2:31:00 AM10/29/14
to ang...@googlegroups.com
got it! Thanks Sander. :)

Kranthi Kiran

unread,
Dec 2, 2014, 8:18:23 AM12/2/14
to ang...@googlegroups.com
adding an ng-if="'true'" inside the ng-include directive did the trick for me. I guess ng-if creates a new scope for the containing element.

In your case, the code would look like -

<ng-include src="'partials/addressform.html'" onload="type='billing';" ng-if="'true'"></ng-include>
 <ng-include src="'partials/addressform.html'" onload="type='delivery';" ng-if="'true'"></ng-include>

Eric Eslinger

unread,
Dec 2, 2014, 9:04:14 AM12/2/14
to ang...@googlegroups.com
Is there a reason you'd do it this way rather than making a custom directive with an isolate scope?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Luiz Rolim

unread,
Jan 21, 2015, 4:10:22 PM1/21/15
to ang...@googlegroups.com
clever solution Kranthi.

thanks

Naren

unread,
May 30, 2017, 1:18:57 PM5/30/17
to Angular and AngularJS discussion
Thanks a lot for the simple solution.
Reply all
Reply to author
Forward
0 new messages