Javascript 将模板url传递给angularjs指令
有没有办法将带有模板url的字符串对象传递给指令? 我的解决方案不能正常工作 Javascript:Javascript 将模板url传递给angularjs指令,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,有没有办法将带有模板url的字符串对象传递给指令? 我的解决方案不能正常工作 Javascript: function moduleView() { return{ restrict: 'A', templateUrl: function (elem, attr) { return attr.url; } }; }; function ModulesCt
function moduleView() {
return{
restrict: 'A',
templateUrl: function (elem, attr) {
return attr.url;
}
};
};
function ModulesCtrl($scope, ModulesService) {
$scope.modules = [];
$scope.run = function () {
ModulesService.loadModules(function (items) {
$scope.modules = items;
});
};
$scope.run();
};
htmljade:
ul(ng-repeat="item in modules")
div(module-view url="item.path")
item.path是一个带有模板url的字符串
但是这个代码不起作用。我有一个错误:
Error: [$compile:tpload] Failed to load template: item.path
有多种方法可以做到这一点——最简单的方法是使用ng include来包含模块的模板 看看这个例子
当然,也可以向指令中添加一个范围变量,并让它与该变量一起执行ng include 返回attr.url;将返回实际字符串item.path,而不是存储在path属性中的url。在返回字符串之前,需要对其进行插值。问题是templateUrl函数在插值发生之前运行。此外,还没有可用的$scope,只有$rootScope。是否从$rootScope引用项?从控制器功能模块中的$scope引用项Ctrl$scope,模块服务{$scope.modules=[];$scope.run=函数{ModuleService.loadModulesfunction项{$scope.modules=items;};};$scope.run;};item是modulestemplate中数组模块ulng repeat=item的元素:然后您必须以另一种方式执行此操作。根据您的用例,您可以使用@HarishR建议的内容。@Egor您为什么要为此编写指令?你的要求和你在这里提到的不同吗??