我正在尝试使用控制器中的自定义过滤器过滤角度js数组
过滤器在某些移动设备上正常工作,而某些移动设备无法正常工作。
请参阅下面的代码
var app = angular.module('myApp', []);
function filter(input, filterarr) {
return input.filter(function(e1){
return filterarr.find(function(e2) {
return e1.sender === e2.sender && e1.receiver === e2.receiver;
});
});
}
app.controller('myCtrl', function($scope) {
var messages = [
{sender:'fred', receiver:'josh', msg:'HI'},
{sender:'josh', receiver:'fred', msg:'i DEY'},
{sender:'josh', receiver:'racheal', msg:'hw re u2?'},
{sender:'barack', receiver:'angela', msg:'Dear'},
{sender:'fred', receiver:'josh', msg:'Hello'},
{sender:'angela', receiver:'barack', msg:'Moin'},
{sender:'josh', receiver:'racheal', msg:'hw re u?'}
];
var name1 = 'fred';
var name2 = 'josh';
var myFilter = [{sender: name1, receiver: name2},{sender: name2, receiver: name1}];
$scope.messages = filter(messages, myFilter);
});
和我的HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="msg in messages">
Sender : {{ msg.sender }} MSg : {{ msg.msg }} Receiver : {{ msg.receiver }}
</div>
</div>
答案 0 :(得分:1)
问题是.find()函数,某些浏览器和操作系统不支持它,所以我不得不在这里使用polyfill函数
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find#Polyfill