简体   繁体   English

以指令名称作为参数的角度指令

[英]angular directive that takes a directive name as a parameter

I am trying to create a list/grid that would know to render its items based on some directive where the row rendering directive would be a parameter of the grid. 我正在尝试创建一个列表/网格,该列表/网格将基于某种指令来渲染其项,其中行渲染指令将是网格的参数。

I was able to create something like this: 我能够创建如下内容:

<div repeat-directive-per-item="directiveName" items="my_items" item-attr="an_item"></div>

By returing a synthesised template from a template function but encountered problems when i tired to use this directive inside another directive. 通过从模板函数重新创建合成模板,但是当我厌倦在另一个指令中使用此指令时遇到了问题。

Before i try to resolve those problems I wanted to ask if this could maybe done in some simpler way. 在尝试解决这些问题之前,我想问一下是否可以用一些更简单的方法来完成。

I am looking for some syntax like this: 我正在寻找这样的语法:

<div ng-repeat="{{itemAttr}} in results" {{rowDirective}} /></div>

but it does not seem like ng-repeat is willing to do this type of interpulation. 但ng-repeat似乎不愿意进行这种插入。

I know I could also limit the pairs of rowDirective and matching itemAttr and do an if on them but it feels less general and less elegant. 我知道我也可以限制对rowDirective和匹配的itemAttr的配对,并在它们上做一个if,但是感觉不太通用,也不太优雅。

You can not create attribute by using {{}} interpolation. 您无法使用{{}}插值创建属性。

While creating template in directive you could use template function of directive, which can help you to add attribute by accessing attribute value. 在指令中创建模板时,可以使用指令的模板功能,该功能可以帮助您通过访问attribute值来添加属性。

template: function(element, attrs){
   return '<div ng-repeat="{{itemAttr}} in results" '+ attrs.repeatDirectivePerItem +' /></div>'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM