Angularjs 用于替换模板的角度模板URL
我一直在尝试加载文件,而不是内联html 我有一个Angularjs 用于替换模板的角度模板URL,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我一直在尝试加载文件,而不是内联html 我有一个 var-app=angular.module('myApp',[]); app.directive('contentItem',函数($compile){ var imageTemplate='{{content.title}}{{content.description}}}'; var videoTemplate='{{content.title}}{{{content.description}}'; var noteTemplate='{{
var-app=angular.module('myApp',[]);
app.directive('contentItem',函数($compile){
var imageTemplate='{{content.title}}{{content.description}}}';
var videoTemplate='{{content.title}}{{{content.description}}';
var noteTemplate='{{content.title}}{{content.data}}';
var getTemplate=函数(contentType){
var模板=“”;
开关(contentType){
案例“图像”:
模板=图像模板;
打破
案例“视频”:
模板=视频模板;
打破
案例“注释”:
模板=注释模板;
打破
}
返回模板;
}
变量链接器=函数(范围、元素、属性){
/*scope.rootDirectory='images/'*/
html(getTemplate(scope.content.content_type))/*已删除“.show()”*/;
$compile(element.contents())(范围);
}
返回{
限制:“E”,
替换:正确,
链接:链接器,
范围:{
内容:'='
}
};
});
函数ContentCtrl($scope,$http){
“严格使用”;
$scope.url='content.json';
$scope.content=[];
$scope.fetchContent=function(){
$http.get($scope.url).then(函数(结果){
$scope.content=result.data;
});
}
$scope.fetchContent();
}
我想知道如何从URL加载模板-load image.html
我试图将模板替换为templateUrl——不幸的是,这并没有起到多大作用
您能告诉我如何正确执行此操作吗请在代码中声明一个指令,但不使用其任何模板选项。 您需要重构代码以使用带有templateUrl选项的指令模板
directive('directiveName', function(){
// Runs during compile
return {
// name: '',
// priority: 1,
// terminal: true,
// scope: {}, // {} = isolate, true = child, false/undefined = no change
// controller: function($scope, $node, $attrs, $transclude) {},
// require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
// restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
// template: '',
// templateUrl: '', //you need it
// replace: true,
// transclude: true,
// compile: function(tElement, tAttrs, function transclude(function(scope){ return function linking(scope, $node, attrs){}})),
link: function($scope, iElm, iAttrs, controller) {
}
};
});
如果您仍然希望模拟模板行为,则应使用$http服务从服务器获取模板html并将其加载*
在这种情况下,您应该创建一个服务或工厂,它是一个单例,并注入$http,并将响应模板存储在一个变量中,这样就不会多次调用服务器。
但是我认为重新编码指令特性是个坏主意。在代码中声明指令,但不使用任何模板选项。 您需要重构代码以使用带有templateUrl选项的指令模板
directive('directiveName', function(){
// Runs during compile
return {
// name: '',
// priority: 1,
// terminal: true,
// scope: {}, // {} = isolate, true = child, false/undefined = no change
// controller: function($scope, $node, $attrs, $transclude) {},
// require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
// restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
// template: '',
// templateUrl: '', //you need it
// replace: true,
// transclude: true,
// compile: function(tElement, tAttrs, function transclude(function(scope){ return function linking(scope, $node, attrs){}})),
link: function($scope, iElm, iAttrs, controller) {
}
};
});
如果您仍然希望模拟模板行为,则应使用$http服务从服务器获取模板html并将其加载*
在这种情况下,您应该创建一个服务或工厂,它是一个单例,并注入$http,并将响应模板存储在一个变量中,这样就不会多次调用服务器。
但是我认为重新编码指令特性是个坏主意。在代码中声明指令,但不使用任何模板选项。 您需要重构代码以使用带有templateUrl选项的指令模板
directive('directiveName', function(){
// Runs during compile
return {
// name: '',
// priority: 1,
// terminal: true,
// scope: {}, // {} = isolate, true = child, false/undefined = no change
// controller: function($scope, $node, $attrs, $transclude) {},
// require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
// restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
// template: '',
// templateUrl: '', //you need it
// replace: true,
// transclude: true,
// compile: function(tElement, tAttrs, function transclude(function(scope){ return function linking(scope, $node, attrs){}})),
link: function($scope, iElm, iAttrs, controller) {
}
};
});
如果您仍然希望模拟模板行为,则应使用$http服务从服务器获取模板html并将其加载*
在这种情况下,您应该创建一个服务或工厂,它是一个单例,并注入$http,并将响应模板存储在一个变量中,这样就不会多次调用服务器。
但是我认为重新编码指令特性是个坏主意。在代码中声明指令,但不使用任何模板选项。 您需要重构代码以使用带有templateUrl选项的指令模板
directive('directiveName', function(){
// Runs during compile
return {
// name: '',
// priority: 1,
// terminal: true,
// scope: {}, // {} = isolate, true = child, false/undefined = no change
// controller: function($scope, $node, $attrs, $transclude) {},
// require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
// restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
// template: '',
// templateUrl: '', //you need it
// replace: true,
// transclude: true,
// compile: function(tElement, tAttrs, function transclude(function(scope){ return function linking(scope, $node, attrs){}})),
link: function($scope, iElm, iAttrs, controller) {
}
};
});
如果您仍然希望模拟模板行为,则应使用$http服务从服务器获取模板html并将其加载*
在这种情况下,您应该创建一个服务或工厂,它是一个单例,并注入$http,并将响应模板存储在一个变量中,这样就不会多次调用服务器。
但是我认为重新编码一个指令特性是个坏主意。首先,您可以使用ng view设置模板URL 这是一个有效的例子
app.directive('contentItem',函数($compile,$parse){
模板={
图片:“image.html”,
视频:“video.html”,
备注:“note.html”
}
变量链接器=函数(范围、元素、属性){
scope.setUrl=函数(){
返回模板[scope.content];
}
}
返回{
限制:“E”,
替换:正确,
链接:链接器,
范围:{
内容:'='
},
模板:“”
};
});
首先,您可以使用ng view设置模板URL
这是一个有效的例子
app.directive('contentItem',函数($compile,$parse){
模板={
图片:“image.html”,
视频:“video.html”,
备注:“note.html”
}
变量链接器=函数(范围、元素、属性){
scope.setUrl=函数(){
返回模板[scope.content];
}
}
返回{
限制:“E”,
替换:正确,
链接:链接器,
范围:{
内容:'='
},
模板:“”
};
});
首先,您可以使用ng view设置模板URL
这是一个有效的例子
app.directive('contentItem',函数($compile,$parse){
模板={
图片:“image.html”,
视频:“video.html”,
备注:“note.html”
}
变量链接器=函数(范围、元素、属性){
scope.setUrl=函数(){
返回模板[scope.content];
}
}
返回{
限制:“E”,
替换:正确,
链接:链接器,
范围:{
内容:'='
},
模板:“”
};
});
首先,您可以使用ng view设置模板URL
这是一个有效的例子