在输入时实施角度材料自动完成搜索

时间:2015-11-02 09:54:29

标签: javascript angularjs material-design angular-material

我已经完成了自动完成功能,单击按钮可以很好地从远程服务器获取所有关键结果。但是问题是在自动完成栏上使用输入功能实现相同的功能。

编辑:autosuggest显示结果完美,我想收集搜索文本并在用户进入搜索框时在新页面上显示完整结果,否则只显示autosuggest中的摘要

navbar.html

...
    <div  ng-controller="AppCtrl as ctrl" >    
    <form ng-submit="$event.preventDefault()" style="width: 100%; background: transparent;" ng-controller="gotoSearchLanding">
                          <md-autocomplete
                              ng-disabled="ctrl.isDisabled"
                              md-no-cache="ctrl.noCache"
                              md-selected-item="ctrl.selectedItem"
                              md-search-text-change=""
                              md-search-text="ctrl.searchText"
                              md-selected-item-change="ctrl.selectedItemChange(item)"
                              md-items="item in ctrl.searchTextChange(ctrl.searchText)"
                              md-item-text="item.name"
                              md-min-length="0"
                              placeholder="Search Data"
                              ng-enter="gotoSearchLandingFun(ctrl.searchText)">>
                              <md-item-template>
                              <span class="item-title">
                                <img ng-src="img/jobs.png" width="20">
                                <span> {{item.name}} </span>
                              </span>
                              <span class="item-metadata">
                                <span class="item-metastat">
                                  <strong>{{item.employee_id}}</strong> 
                                </span>
                                <span class="item-metastat">
                                  <strong>{{item.email}}</strong> 
                                </span>
                              </span>
                            </md-item-template>
                          </md-autocomplete>
                        </form>
                        <span ng-controller="gotoSearchLanding">
                         <md-button class="md-fab md-mini" style="background-color:#fff" aria-label="Eat cake"  ng-click="gotoSearchLandingFun(ctrl.searchText)">
                            <ng-md-icon icon="search"></ng-md-icon>
                        </md-button>   
                        <span>
    </div>

controller.js

/**
 * Auto Search App Controller
 */

angular.module('AppTool')
  .controller('AppCtrl', [ '$http', '$state', AppCtrl]);
  function AppCtrl ($http, $state) {
    var self = this;      
    self.simulateQuery = false;
    self.isDisabled    = false;
    self.querySearch   = querySearch;
    self.selectedItemChange = selectedItemChange;
    self.searchTextChange   = searchTextChange;
    function querySearch (query) {
      var results = query ? self.repos.filter( createFilterFor(query) ) : self.repos,
          deferred;
      if (self.simulateQuery) {
        deferred = $q.defer();
        $timeout(function () { deferred.resolve( results ); }, Math.random() * 1000, false);
        return deferred.promise;
      } else {

        return results;
      }
    }

    function searchTextChange(text) {
     return $http.get('http://localhost:3000/search', {
        params: {
          q: text
        }
      }).then(function(response){
        return response.data.map(function(item){
          return item._source;
        });
      }, function (error) {
          console.log("error");
      });
    }

    function selectedItemChange(item) {
    }

    function createFilterFor(query) {
      var lowercaseQuery = angular.lowercase(query);
      return function filterFn(item) {
        return (item.value.indexOf(lowercaseQuery) === 0);
      };
    }
}

gotsearchlandingCtrl.js

angular.module('AppTool')
    .controller('gotoSearchLanding', ['$scope','$state', gotoSearchLanding]);

function gotoSearchLanding($scope, $state) {

 $scope.gotoSearchLandingFun = function($val) {
        alert($val);  
        $state.go("searchLanding", { searchVal: $val});
    };     
}

directive.js

angular.module('PdbTool')
.directive('myEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                scope.$apply(function (){
                    scope.$eval(attrs.myEnter);
                });
                event.preventDefault();
            }
        });
    };
});

2 个答案:

答案 0 :(得分:1)

在md-autocomplete标记中使用您的指令myEnter。

这是我修改后的代码:

directives.myEnter = function () {
  return function (scope, element, attrs) {
      element.bind("keydown keypress", function (event) {
          if(event.which === 13) {
              scope.$apply(function () {
                  scope.gotoSearchLandingFun(scope.searchText);
              });
              event.preventDefault();
          }
      });
  };

}

和自动填充HTML:

<md-autocomplete  class="search_box"
                          md-selected-item="selectedItem"
                          md-search-text="searchText"
                          md-items="item in querySearch(searchText)"
                          md-search-text-change="querySearch(searchText)"
                          md-selected-item-change="search(searchText)"
                          md-item-text="item.value"
                          md-min-length="2"
                          md-autofocus="true"
                          md-no-cache="false"
                          placeholder="Search..." my-enter>
            <md-item-template>
                <span>{{item.value}}</span>
            </md-item-template>
            <md-not-found>
                No matches found.
            </md-not-found>
        </md-autocomplete>

由于myEnter范围属于主要功能的范围,您可以调用它们,如示例中所示。

答案 1 :(得分:0)

如果您使用以下自动填充属性,则可以使用Enter键选择表单上的项目:

md-require-match="true"
md-select-on-match="true"
md-match-case-insensitive="true"
md-selected-item="selectedItem"