在控制器中使用角度过滤器过滤非匹配对象
问题描述:
我正在使用角度$过滤器从两个字符串中取得不匹配的对象。在控制器中使用角度过滤器过滤非匹配对象
我写下面的代码来迭代。
angular.forEach(this.members, function (value: any, index: any) {
var person = this.$filter('filter')(this.allPersonnel, { Id: value.Member.Id })[0];
console.log(person);
this.validPerson.push(person); // Filter
});
正如你看到的我是不是不能加“不等于”条件。我试过"Id:'!value.Member.Id'"
但它正在考虑作为字符串。
难道你们请告诉我一种方法来做到这一点。我正在使用Typescript角。
答
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<p><input type="button" value="Find Match" ng-click="FindUniqueRecords()"/></p>
<p> Filtered Records:</p>
<ul>
<li ng-repeat="x in FilteredRecords">
{{ x }}
</li>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl',['$scope','$filter', function($scope,ff) {
$scope.FilteredRecords=[];
$scope.FindUniqueRecords=function(){
angular.forEach($scope.CountryCode1,function(value,index){
var foundMatch=ff('filter')($scope.CountryCode2,value);
if(foundMatch.length==0){
$scope.FilteredRecords.push(value);
}
});
angular.forEach($scope.CountryCode2,function(value,index){
var foundMatch=ff('filter')($scope.CountryCode1,value);
if(foundMatch.length==0){
$scope.FilteredRecords.push(value);
}
});
};
$scope.CountryCode1= [
'Aus',
'NZ'
];
$scope.CountryCode2= [
'Aus',
'Ind',
'Eng'
];
}]);
</script>
</body>
</html>
我想这个逻辑从两个阵列找到独特的名单。
感谢您的回复。我正在寻找逻辑来查找两个数组中不匹配的节点。我知道如何创建自定义过滤器。我看起来像C#linq - 'var list1IDs = new HashSet(List1.Select(s => s.ID)); var results = List2.Where(m =>!list1IDs .Contains(m.ID));' –
Ahh OK。那么最简单的方法就是使用像Lodash这样的库。在那里你有一个像https://lodash.com/docs/#differenceBy这样的方法,你可以把你的逻辑和想要的结果。 – tomepejo
这里是如何将Lodash导入到您的项目中http://stackoverflow.com/questions/34660265/importing-lodash-into-angular2-typescript-application。 – tomepejo