指令:在 templateUrl 函数中获取 attr scope-value [英] Directive: Get attr scope-value in the templateUrl function

查看:17
本文介绍了指令:在 templateUrl 函数中获取 attr scope-value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要构建的指令:

module.directive('templater', function () {返回 {限制:'A',替换:真的,templateUrl:函数(元素,属性){返回 attrs.templater;}};});

但是,如您所知,在此 HTML 中:

访问 attrs.templater 只是提供 item.template 而不是实际的模板 url 字符串.

如何在不进入链接函数的情况下访问 attrs.templater 中的数据?我想利用 templateUrl 函数的简单性,同时避免链接函数中的回流开销.

解决方案

我最初的假设是错误的.

由于 item.template 仅在范围的上下文中有意义,并且由于模板获取/编译发生在没有有关范围的信息的时间点,因此似乎没有是在链接阶段之前获取模板的一种方式.

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.

解决方案

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.

这篇关于指令:在 templateUrl 函数中获取 attr scope-value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆