我可以使用另一个下拉列表删除所选选项 angularjs 1.但我如何使用angular2过滤器使用相同的逻辑?
任何人都可以帮助使用angular 2编写过滤器吗?
<div ng-app="myApp" ng-init="foos = [{label:'label 1',id:1},{label:'label 2',id:2},{label:'label 3',id:3}]; selected = []">
<div ng-repeat="foo in foos">
<select ng-model="selected[$index]" ng-options="obj.id as obj.label for obj in foos | customFilter:selected:selected[$index]"></select>
</div>
</div>
angular.module('myApp', [])
.filter('customFilter', function(filterFilter) {
return function(input, filterEach, exclude) {
filterEach.forEach(function(item) {
if (angular.equals(item, exclude)) { return; }
input = filterFilter(input, '!'+item);
});
return input;
};
});
答案 0 :(得分:1)
我认为使用过滤器构建它不是更明确和明确地将选项绑定到方法,而是返回未选择的选项...类似于:
getOptions() {
return this.model.filter((el)=>{
return !(el in this.selectedOptions);
})
}
答案 1 :(得分:-2)
import {Pipe, PipeTransform} from "@angular/core";
@Pipe({name: 'arrayDiff', pure: false})
export class ArrayDiffPipe implements PipeTransform {
public array1:any=[];
transform(array: Array<any>, diff: Array<any>): Array<any> {
var i, item,
newArray = [],
exception = Array.prototype.slice.call(arguments, 2);
if (array && Array.isArray(array)) {
for (i = 0; i < array.length; i++) {
item = array[i].description;
if(diff.indexOf(item) < 0 || exception.indexOf(item) >= 0) {
newArray.push(item);
}
}
console.log(newArray, 'newArray');
return newArray;
}
}
}