【发布时间】:2016-05-19 21:28:59
【问题描述】:
这是我的问题的想法:
想象一下,我有一个用户正在寻找一本书。
作为输入,他可以尝试使用书名来查找书籍。
让我们来:
$scope.books = ["Heir Of Earth",
"Woman Of The Solstice",
"Hunters Of Next Year",
"Pilots Without Honor",
"Slaves And A Woman",
"Kings And Heroes",
"Decay Of The End",
"Fortune Of The Land",
"Rejecting My Dreams",
"Painting The Angels"];
现在我将用户查询放在一个字符串中:
示例:
$scope.userQueryTitle = "Woman Of";
我想获取标题包含“Woman”或“Of”但不包含“Woman”和“Of”的所有书籍。
从那里,相应的结果应该是这样的:
$scope.booksResult = ["Heir Of Earth",
"Woman Of The Solstice",
"Hunters Of Next Year",
"Slaves And A Woman",
"Decay Of The End",
"Fortune Of The Land"
];
我找到了一些部分解决我的问题的方法,例如使用类似的东西:
查看:
<ul ng-repeat="book in booksResult | filter: getBooksFromTitle">
<li>{{book}}</li>
</ul>
控制器:
$scope.getBooksFromTitle = function(book){
return book.match(($scope.userQueryTitle).split(" ")[0]) ||
book.match(($scope.userQueryTitle).split(" ")[1]);
};
仅当 $scope.userQueryTitle 仅包含 2 个单词但 我的问题是 如果我有 1 或 4 或 怎么办>n 个单词?
getBooksFromTitle() 应该对此很灵活,但我不知道如何处理......
有什么建议吗?
谢谢!
【问题讨论】:
-
您可以尝试以下方法...在您的 getBooksFromTitle 方法中,您可以将 $scope.userQueryTitle 转换为数组并比较两个数组。
标签: javascript angularjs angularjs-ng-repeat angular-filters