Javascript 指令内模板URL的动态加载
我在指令中以正确的模板url值为目标,正如我的指令console.log()所示,但模板没有加载 指令:Javascript 指令内模板URL的动态加载,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我在指令中以正确的模板url值为目标,正如我的指令console.log()所示,但模板没有加载 指令: app.directive('customtemp', function($parse) { var x=""; return { scope: { tempUrl:"=" }, link: function(scope, element, attrs) { console.log(scope.
app.directive('customtemp', function($parse) {
var x="";
return {
scope: {
tempUrl:"="
},
link: function(scope, element, attrs) {
console.log(scope.tempUrl);
x = scope.tempUrl;
},
templateUrl: x
}
});
模板:
<div ng-init="template = attobj.template">
<customtemp temp-url="template">
</customtemp>
</div>
我在这里遗漏了什么,templateUrl的第二次返回?您可以使用ng include来完成此操作,我认为您不能像以前那样使用它
link: function(scope, element, attrs) {
scope.getContentUrl = function() {
return attrs.tempUrl;
}
},
template: '<div ng-include="getContentUrl()"></div>'
链接:函数(范围、元素、属性){
scope.getContentUrl=函数(){
返回attrs.tempUrl;
}
},
模板:“”
I allready有一个使用ng include的版本,我不希望在嵌套的ng repeats中使用它,因为性能会在窗口中消失。加载模板的指令非常快。静态url可以很好地工作,但我无法让它与动态url一起工作。非常确定我没有正确设置Directive的返回结构。另一种方法是在link函数中编译html模板并将其添加到div中。类似于我在本文中的回答。这对你的需要有意义吗?您是否有预构建模板,或者您无法在运行中构建模板?预构建模板作为