Ale*_*ana 169 angularjs angularjs-directive
我在routeProvider模板中有一个自定义标记,用于调用directive模板.该version范围将填充该属性,然后调用正确的模板.
<hymn ver="before-{{ week }}-{{ day }}"></hymn>
Run Code Online (Sandbox Code Playgroud)
这首赞美诗有多个版本,基于它的周和日.我期待使用该指令来填充正确的.html部分.该变量未被读取templateUrl.
emanuel.directive('hymn', function() {
var contentUrl;
return {
restrict: 'E',
link: function(scope, element, attrs) {
// concatenating the directory to the ver attr to select the correct excerpt for the day
contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html';
},
// passing in contentUrl variable
templateUrl: contentUrl
}
});
Run Code Online (Sandbox Code Playgroud)
有在摘录目录多个文件被标记before-1-monday.html,before-2-tuesday.html...
And*_*rin 311
emanuel.directive('hymn', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
// some ode
},
templateUrl: function(elem,attrs) {
return attrs.templateUrl || 'some/path/default.html'
}
}
});
Run Code Online (Sandbox Code Playgroud)
所以你可以通过标记提供templateUrl
<hymn template-url="contentUrl"><hymn>
Run Code Online (Sandbox Code Playgroud)
现在,您只需注意属性contentUrl使用动态生成的路径填充.
pgr*_*ory 183
你可以使用ng-include指令.
尝试这样的事情:
emanuel.directive('hymn', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
scope.getContentUrl = function() {
return 'content/excerpts/hymn-' + attrs.ver + '.html';
}
},
template: '<div ng-include="getContentUrl()"></div>'
}
});
Run Code Online (Sandbox Code Playgroud)
UPD.用于观看ver属性
emanuel.directive('hymn', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
scope.contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html';
attrs.$observe("ver",function(v){
scope.contentUrl = 'content/excerpts/hymn-' + v + '.html';
});
},
template: '<div ng-include="contentUrl"></div>'
}
});
Run Code Online (Sandbox Code Playgroud)
感谢@pgregory,我可以使用此指令解决我的问题以进行内联编辑
.directive("superEdit", function($compile){
return{
link: function(scope, element, attrs){
var colName = attrs["superEdit"];
alert(colName);
scope.getContentUrl = function() {
if (colName == 'Something') {
return 'app/correction/templates/lov-edit.html';
}else {
return 'app/correction/templates/simple-edit.html';
}
}
var template = '<div ng-include="getContentUrl()"></div>';
var linkFn = $compile(template);
var content = linkFn(scope);
element.append(content);
}
}
})
Run Code Online (Sandbox Code Playgroud)
你这里不需要自定义指令.只需使用ng-include src属性.它已编译,因此您可以将代码放入其中.请参阅plunker解决方案以解决您的问题.
<div ng-repeat="week in [1,2]">
<div ng-repeat="day in ['monday', 'tuesday']">
<ng-include src="'content/before-'+ week + '-' + day + '.html'"></ng-include>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
176835 次 |
| 最近记录: |