简体   繁体   中英

How does a valuesFn in angular js work?

I am trying to implement the below code for search and select. http://plnkr.co/edit/o767Mg6fQoyc7jKq77If?p=preview

Instead of a string, I am trying to pass an object, and so the search functionality searches on the entire object. However, I want to search only on a particular key.

The search particularly happens at

    function getMatches(searchTerm) {
                var locals = { $searchTerm: searchTerm }
                $q.when(valuesFn(scope, locals)).then(function (matches) {
                    if (!matches) return;

                    if (searchTerm === inputElement.val().trim()/* && hasFocus*/) {
                        matchMap = {};
                        childScope.matches.length = 0;
                        for (var i = 0; i < matches.length; i++) {
                            locals[valueName] = matches[i];
                            var value = valueFn(scope, locals),
                                label = displayFn(scope, locals);

                            matchMap[hashKey(value)] = {
                                value: value,
                                label: label/*,
                                model: matches[i]*/
                            };

                            childScope.matches.push(matches[i]);
                        }
                        //childScope.matches = matches;
                    }

                    if (needsDisplayText) setDisplayText();
                }, function() {
                    resetMatches();
                });
            }

What I see is that the search happens on line 3 of above code snippet in

valuesFn(scope, locals)

I was wondering what this function valuesFn(scope,locals) is and how I can only do a search on a object key instead of entire object.

This is not an answer to the actual question of how the ValuesFn() works. I am posting here a way around for the solution of filtering the object on key. I used the angular JS filter function in the $q.when method. ie

$filter('filter')(scope.attributeItemLevel, { name: locals.$searchTerm })

The entire function looks like this.

function getMatches(searchTerm) {
            var locals = { $searchTerm: searchTerm }
            $q.when($filter('filter')(scope.attributeItemLevel, { name: locals.$searchTerm }))).then(function (matches) {
                if (!matches) return;

                if (searchTerm === inputElement.val().trim()/* && hasFocus*/) {
                    matchMap = {};
                    childScope.matches.length = 0;
                    for (var i = 0; i < matches.length; i++) {
                        locals[valueName] = matches[i];
                        var value = valueFn(scope, locals),
                            label = displayFn(scope, locals);

                        matchMap[hashKey(value)] = {
                            value: value,
                            label: label/*,
                            model: matches[i]*/
                        };

                        childScope.matches.push(matches[i]);
                    }
                    //childScope.matches = matches;
                }

                if (needsDisplayText) setDisplayText();
            }, function() {
                resetMatches();
            });
        }

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