简体   繁体   中英

AngularJS search input with operators

I'm trying to make a search on my website that makes use of operators (OR and AND). I have an array like this:

$scope.data = ['orange', 'apple', 'banana']

I have an input field with a model. I want a user to be able to search in this input field with "orange OR banana" and it should return both orange and banana. Basically the same as: http://angular.github.io/angular-phonecat/step-3/app/ but this doesn't accept operators.

This seems to be a very trivial question but I can't find an answer to this or an working example...

In the particular example that you've provided, both "orange OR banana" and "orange AND banana" would return the same result set. Seem like it's really an OR search with multiple tokens that you're after. Tokenize the query and match against the data set (or something more complex).

You could use a custom expression to do the matching you need; something along the lines of data | filter: findMatch(searchInput) data | filter: findMatch(searchInput)

And put the function in scope

$scope.findMatch = function findMatch(query) {
    return function (item) {
        //TODO: comparison logic, return true|false
    }
}

View example on Plunker

Have a look at Angular doc for further detail on the filter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM