0

在 angularjs 中创建过滤器时,如何访问数组中的前一项?即我想将当前元素与前一个元素进行比较

4

1 回答 1

1

Hard to figure out exactly what you're trying to achieve, but I think if you're using an ng-repeat you can do this:

HTML:

<div data-ng-repeat="element in elements">
    <div>{{ element | myFilter:elements:$index</div>
</div>

Javascript

.filter("myFilter", function(){
    return function(input, elements, index){

        if(index > 0){
            var previousElement = elements[index - 1]
        }

        return input;
    }
});

Edit

Ok now that you've specified what you're trying to do, you can use a filter in a different way to instead remove the duplicate dates.

Here's the filter:

.filter("uniqueDates", function(){
    return function(input){
      var returnInput = [];
        for(var x = 0; x < input.length; x++){
            if(returnInput.indexOf(input[x]) === -1){
                returnInput.push(input[x]);
            }
        }

      return returnInput;  
    };
})

Here's a jsfiddle with the html and everything all wired up that you can extrapolate from: http://jsfiddle.net/nE9EE/

于 2013-04-18T17:35:40.433 回答