Directive: Get attr scope-value in the templateUrl function

对着背影说爱祢 提交于 2019-12-11 03:39:16

问题


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:


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.



来源:https://stackoverflow.com/questions/22911540/directive-get-attr-scope-value-in-the-templateurl-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!