Angular.js指令动态templateURL

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使用动态生成的路径填充.

  • 最后!正是我在寻找的!我没有意识到我可以从templateUrl函数访问elem和attrs.谢谢! (11认同)
  • 每个指令调用一次templateUrl,在每个指令实例初始化时都没有调用它,小心!!! 它可能是有角度的bug但是...... (7认同)
  • 很好,但是...我可以从templateUrl函数访问范围属性吗?templateUrl取决于范围值,但我无法访问它:( (4认同)
  • 我还没有检查过,但根据我最新的调查结果,可能值得一提的是,每个$ compile阶段都有一次.换句话说,如果你对你的指令使用`ng-repeat`并想要根据特定的`ng-repeat`项上下文设置单独的模板,它将无法工作,因为`$ compile`阶段在实际之前遍历你的指令一次`ng-repeat`发生了.所以在那意味着它被称为一次...... (2认同)

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)


Shi*_*lan 6

感谢@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)


ice*_*cem 5

你这里不需要自定义指令.只需使用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)