我想动态地初始化一系列函数,
示例:
转换以下代码:
$filter('orderBy')(datas,
[
function (obj) {
return filterTable(obj, sortInfos.fields[0]);
},
function (obj) {
return filterTable(obj, sortInfos.fields[1]);
},
function (obj) {
return filterTable(obj, sortInfos.fields[2]);
},
function (obj) {
return filterTable(obj, sortInfos.fields[3]);
}
],
revert);
致:
$filter('orderBy')(datas, myArrayOfFunction, revert);
答案 0 :(得分:1)
function createMyFuntion(sortInfoIndex) {
return function(obj) { return filterTable(obj, sortInfos.fields[sortInfoIndex]); };
}
var myArrayOfFunction = [];
for (var i = 0; i < 4; i++) {
myArrayOfFunction.push(createMyFuntion(i))
}
$filter('orderBy')(datas, myArrayOfFunction, revert);