我正在尝试制作一些通用字段并使用指令。例如,在HTML代码我定义:将指令中的值传递给编译函数
<div def-field="name"></div>
<div def-field="surname"></div>
<div def-field="children"></div>
该字段可以是两种类型:或者简单元件(如前两个)或元素的列表(如第三个)。范围变量包含所有字段及其类型的定义。 对于我创建的指令“DEF场”:
app.directive("defField", function($compile, $parse, $http) {
restrict: 'A', // only for attributes
scope : true,
return {
restrict: 'A', // only for attributes
scope : true,
compile: function compile(tElement, tAttributes) {
//here I need to detect, which type of field is it.
//if it is array, I need to execute the compile code
if(fieldType === 'array') {
//execute secial code for compile furnction
}
}
if(fieldType === 'array') {
//return for array
var returnValue = {pre : linkFunction};
} else {
//return for normal type
var returnValue = {
pre : linkFunction,
post: function(scope, element, attrs){
$compile(element.parent())(scope);
}
};
}
return returnValue;
}
的问题是,我需要从范围变量和范围变量的字段类型是不是在编译功能可用。是否有可能解决此问题?
目前,我作为属性传递类型“数组”,但这不是一个可以接受的选项。