Re: [angular.js] Dynamic templates for ng-include

4,727 views
Skip to first unread message

Pawel Kozlowski

unread,
Jun 23, 2012, 5:36:15 AM6/23/12
to ang...@googlegroups.com
Hi Dylan!

On Sat, Jun 23, 2012 at 7:06 AM, dylst <dy...@subvertical.com> wrote:

> I'm also in a ng-repeat directive, so I was hoping I'd be able to include
> dynamically based off of a bound value.
> In this example, {{foo.bar}} would return "text":
> <div ng-include src="'/assets/something_{{foo.bar}}.html'"></div>

Try this:

<div ng-include src="'/assets/something_' + foo.bar + '.html'"></div>

here is the example in jsFiddle:
http://jsfiddle.net/pkozlowski_opensource/gxneq/2/

> The first example above works, but the second doesn't.  Is the ng-include
> firing before the interpolation happens?
> I tried a bunch of different quoting scheme's, but nothing seemed to work.

The bottom line is that you shouldn't use curly braces in angular
expressions (http://docs.angularjs.org/guide/expression). It is
helpfull to think about those expressions as JS expressions (althought
there are differences).

Hope this helps,
Pawel

dylan...@gmail.com

unread,
Jun 25, 2012, 5:02:18 PM6/25/12
to ang...@googlegroups.com
Pawel!

Ah, you got it!  That's a great tidbit about expressions, and I'll definitely revisit the doc.
It ended up being simple concatenation in an ng-include:  src="'/assets/something_' + foo.bar + '.html'"
 
Thanks again!
==
Dylan
Reply all
Reply to author
Forward
0 new messages