Directive: Get attr scope-value in the templateUrl

2019-07-17 20:17发布

This is the directive that I wanna build:

module.directive('templater', function () {
return {
    restrict: 'A',
    replace: true,
    templateUrl: function (element, attrs) {
        return attrs.templater;
    }
};
});

but, as you may know, in this HTML:

<div 
    ng-repeat="item in items" 
    templater="item.template">
</div>

accessing attrs.templater simply gives item.template instead of the actual template url string.

How do you access the data inside attrs.templater without going inside the linking function? I want to leverage the simplicity of the templateUrl function, also, avoid the reflow overhead in the linking function.

1条回答
闹够了就滚
2楼-- · 2019-07-17 21:09

EDITED: My original assumption was wrong.

Since item.template is meaningful only in the context of a scope and since the template-fetching/compiling happens at a point in time when there is no info regarding scopes there doesn't seem to be a way to fetch the template prior to the linking phase.

查看更多
登录 后发表回答