Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 指令:在templateUrl函数中获取attr范围值_Angularjs_Angularjs Directive_Angularjs Scope_Angularjs Ng Repeat - Fatal编程技术网

Angularjs 指令:在templateUrl函数中获取attr范围值

Angularjs 指令:在templateUrl函数中获取attr范围值,angularjs,angularjs-directive,angularjs-scope,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Scope,Angularjs Ng Repeat,这是我想要建立的指令: module.directive('templater', function () { return { restrict: 'A', replace: true, templateUrl: function (element, attrs) { return attrs.templater; } }; }); 但是,您可能知道,在本HTML中: <div ng-repeat="item in items"

这是我想要建立的指令:

module.directive('templater', function () {
return {
    restrict: 'A',
    replace: true,
    templateUrl: function (element, attrs) {
        return attrs.templater;
    }
};
});
但是,您可能知道,在本HTML中:

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

访问
attrs.templater
只会给出
item.template
,而不是实际的模板url字符串

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

编辑:我最初的假设是错误的


由于
item.template
仅在作用域的上下文中有意义,并且由于模板获取/编译发生在没有作用域相关信息的时间点,因此似乎没有办法在链接阶段之前获取模板。

然后像attrs.templater一样访问它?我仍然以字符串的形式得到它,就像这样:“{item.template}}”我刚刚检查过,这种计算是在链接函数中进行的。这意味着如果我在链接函数中访问'attrs.templater',我会得到计算过的数据,但不会在templateUrl函数中得到。@Birowsky:恐怕你是对的。在链接之前似乎没有办法访问这些数据。因此,基于此,你同意我应该在链接函数中做我想做的事情吗?@Birowsky:我看不到任何其他可能的方法。