27

I have the following code:

<div ng-repeat="module in modules" id="{{module.Id}}">
    <ng-include ng-init="bootstrapModule(module.Id)" src=""></ng-include>
</div>

I want to be able to build a string in src like so:

/modules/{{module.Name}}/{{module.Name}}.tpl.html

But I keep hitting roadblocks. I've tried to use a call back function to build it,

$scope.constructTemplateUrl = function(id) {
    return '/modules/' + id + '/' + id + '.tpl.html';
}

But this gets called over & over & over and it doesn't seem to like that. I've also tried to construct it like so:

ng-src="/modules/{{module.Id}}/{{module.Id}}.tpl.html"

But that isn't working either. Rather than spend hours beating around the bush, I wondered if anyone else has come up against something like this and has any ideas?

Also, when I grab the modules from $resource, I am returning them asynchronously with $q, so I can't seem to go through and add it into the modules before in the controller as $scope.modules just equals a then function at that point.

Any ideas?

Charles
  • 50,943
  • 13
  • 104
  • 142
Adam K Dean
  • 7,387
  • 10
  • 47
  • 68

1 Answers1

59

ngInclude | src directive requires an angular expression, which means you should probably write

ng-src="'/modules/' + module.Id + '/tpl.html'"

From http://docs.angularjs.org/api/ng.directive:ngInclude

ngInclude|src string angular expression evaluating to URL. If the source is a string constant, make sure you wrap it in quotes, e.g. src="'myPartialTemplate.html'".

It might be better if you construct the url in model instead of inline HTML

<div ng-repeat="module in modules" id="{{module.Id}}">
    <ng-include src="module.url"></ng-include>
</div>
rogerz
  • 1,073
  • 9
  • 18
  • 3
    Your method works now, but I was sure I tried it before, I must have been using ng-src instead of just src. Thanks @rogerz! – Adam K Dean Sep 24 '13 at 10:20
  • @rogerz - This works for me too. In my example it works but I get a 404 error in the console. See answer to:http://stackoverflow.com/a/33299027/828282 Any ideas? Thanks – Paul Oct 23 '15 at 09:49