如何动态地构建一个NG-包括SRC?构建一个、动态、SRC、NG

2023-09-13 02:52:39 作者:待在绿匣里的猫

我有以下的code:

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

我希望能够在SRC建立一个字符串,像这样:

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?

此外,当我抓住从$资源的模块,我用异步$ Q返回他们,所以我似乎无法通过,并在控制器之前将其添加到模块为 $ scope.modules 只是在这一点等于然后功能。

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.

任何想法?

推荐答案

ngInclude | SRC 指令要求的角度前pression,这意味着你应该写

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

NG-SRC =/模块/+ module.Id +'/tpl.html'

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

ngInclude | SRC串角前pression评估到URL。如果  源是一个字符串常量,确保你把它包在引号中,例如  SRC ='myPartialTemplate.html'。

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'".

如果您构造URL模型,而不是内联可能会更好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>