我正在嘗試製作一些通用字段並使用指令。例如,在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;
}
的問題是,我需要從範圍變量和範圍變量的字段類型是不是在編譯功能可用。是否有可能解決此問題?
目前,我作爲屬性傳遞類型「數組」,但這不是一個可以接受的選項。