gpt4 book ai didi

javascript - 如何在 NG-repeat 中调用函数

转载 作者:行者123 更新时间:2023-11-30 17:23:11 24 4
gpt4 key购买 nike

我有一个小问题,我希望在 ng-repeat 中调用一个函数,而不是像这个例子那样,函数删除重复值,但它并不像我希望的那样工作,我的意思是它从字符串中删除重复项并且不是来自循环。

这是HTML代码,“UNIQUE”功能是去除重复内容。

<tr ng-repeat="compet in discipline.compets">
<td class="col-sm-2 feiCenterAlign"><label>{{unique(compet.eventCode)}}</label></td> <td class="col-sm-2 feiCenterAlign"><div class="col-sm-8 col-sm-offset-2"><input type="number" ax-numeric-integer class="form-control input-sm" ng-model="compet.fhi.presented"></div></td>
<td class="col-sm-2 feiCenterAlign"><div class="col-sm-8 col-sm-offset-2"><input type="number" ax-numeric-integer class="form-control input-sm" ng-model="compet.fhi.box"></div></td>
<td class="col-sm-2 feiCenterAlign"><div class="col-sm-8 col-sm-offset-2"><input type="number" ax-numeric-integer class="form-control input-sm" ng-model="compet.fhi.notAccepted"></div></td>
<td class="col-sm-2 feiCenterAlign"><div class="col-sm-8 col-sm-offset-2"><input type="number" ax-numeric-integer class="form-control input-sm" ng-model="compet.fhi.withdrawn"></div></td>
<td class="col-sm-2 feiCenterAlign"><div class="col-sm-8 col-sm-offset-2"><input type="number" ax-numeric-integer class="form-control input-sm" ng-model="compet.fhi.accepted"></div></td>
</tr>

这是“唯一”功能代码。

unique: function(array) { 
var len = array.length;
var out = [];
var obj = {};
for (var i = 0; i < len; i++) {
obj[array[i]] = 0;

}
for (var j in obj) {
out.push(j);
}

return out;
},

非常感谢你的帮助

PS:这是截图链接:hpics.li/d44d002。RED cercle 是“compet.eventCode”。GREEN cercle 是值,我只想显示每个重复值一次。

最佳答案

Angular UI 有一个 filter for removing duplicates :

<tr ng-repeat="compet in discipline.compets | unique:'field name'">

编辑:这是一种重新发明轮子,但由于您无法将 Angular UI 库添加到您的项目中,您可以手动添加 unique 过滤器(来自原始 Angular UI 源代码):

'use strict';

/**
* Filters out all duplicate items from an array by checking the specified key
* @param [key] {string} the name of the attribute of each object to compare for uniqueness
if the key is empty, the entire object will be compared
if the key === false then no filtering will be performed
* @return {array}
*/
angular.module('myApp', []).filter('unique', ['$parse',
function ($parse) {

return function (items, filterOn) {

if (filterOn === false) {
return items;
}

if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) {
var newItems = [],
get = angular.isString(filterOn) ? $parse(filterOn) : function (item) {
return item;
};

var extractValueToCompare = function (item) {
return angular.isObject(item) ? get(item) : item;
};

angular.forEach(items, function (item) {
var isDuplicate = false;

for (var i = 0; i < newItems.length; i++) {
if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
newItems.push(item);
}

});
items = newItems;
}
return items;
};
}
]);

关于javascript - 如何在 NG-repeat 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24783343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com