我无法显示多值表单文本框,即输入的值与我们需要显示匹配值的数组值匹配,对于我只显示一个值。请帮我找出解决方案。 在这个例子中,如果输入“ball”它显示,如果我们输入“ball all”它必须同时显示“ball”“all”但是因为它没有显示bcoz这里我们不返回一个数组如果我赋值给数组,错误。任何人都可以帮我解决我犯的错误。使用过滤器显示数组中多个匹配值
var app = angular.module('angularPracticeApp');
app.controller('AppController', function ($scope) {
$scope.match = {};
$scope.words = [
{ title: "ball", type: 'object' },
{ title: "wall", type: 'object' },
{ title: "all", type: 'word' },
{ title: "alloy", type: 'material' }
];
});
app.filter('exact', function(){
return function(items, match){
var matching = [], matches,resultArray = [];
angular.forEach(items, function(item){ // e.g. { title: "ball" }
matches = true;
angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
\t var stringArray = value.split(/(\s+)/);
\t console.log(stringArray);
\t for(var i=0;i<stringArray.length;i++){
\t \t if(!!value){ // do not compare if value is empty
matches = matches && (item[key] === stringArray[i]);
/* if(item[key] === stringArray[i]){
\t \t \t console.log(stringArray[i]);
resultArray.push(stringArray[i]);
}*/
}
\t }
});
if(matches){
\t console.log(resultArray);
matching.push(item);
}
});
return matching;
}
});
<!doctype html>
<html ng-app="plunker">
<head>
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
<link rel="stylesheet" href="main.css" />
</head>
<body ng-controller="AppController">
<div>
Find words that exactly match title:
<input ng-model="match.title" />
<br>
<table>
<tr ng-repeat="word in words | exact:match">
<td>{{word.title}}</td>
</tr>
</table>
</div>
</body>
</html>
谢谢那仁穆拉利它的正常工作。 – raj
@raj不客气:) –