如何动态构建一个包含ng的src?

时间:2022-08-25 23:54:19

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:

我想在src中建立一个字符串

/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:

但这被称为over & over,而且它似乎不喜欢这样。我也试着这样构建它:

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.

而且,当我从$resource中获取模块时,我将以$q异步返回它们,因此我似乎不能在控制器中作为$scope将其添加到控制器中。在那个点上,模块就等于一个函数。

Any ideas?

什么好主意吗?

1 个解决方案

#1


56  

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

ngInclude | src指令需要一个角表达式,这意味着您应该编写

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

ng-src = " /模块/ +模块。Id + / tpl.html”

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

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

ngInclude|src字符串的角表达式计算到URL。如果源是字符串常量,请确保用引号括起来,例如src=" myPartialTemplate.html "。

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

如果您在模型中构建url而不是内联HTML,可能会更好

<div ng-repeat="module in modules" id="{{module.Id}}">
    <ng-include src="module.url"></ng-include>
</div>

#1


56  

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

ngInclude | src指令需要一个角表达式,这意味着您应该编写

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

ng-src = " /模块/ +模块。Id + / tpl.html”

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

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

ngInclude|src字符串的角表达式计算到URL。如果源是字符串常量,请确保用引号括起来,例如src=" myPartialTemplate.html "。

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

如果您在模型中构建url而不是内联HTML,可能会更好

<div ng-repeat="module in modules" id="{{module.Id}}">
    <ng-include src="module.url"></ng-include>
</div>