【问题标题】:how to use ng-change in angular typeahead search如何在角度预输入搜索中使用 ng-change
【发布时间】:2016-01-30 11:36:59
【问题描述】:

我想在按键事件中搜索用户列表。我使用customTemplate.js 显示列表,但第一次按键结果列表没有显示。

这是我的代码:

<input type="text" placeholder="Search people here..." ng-change="getMatchedUser();"  ng-model="selected" data-typeahead="user as user.name for user in searchMembers | filter:$viewValue" typeahead-on-select="onSelect($item, $model, $label)" typeahead-template-url="customTemplate.html"  /> 

<script type="text/ng-template" id="customTemplate.html">
                <a style="cursor: pointer;">
                    <div class="search-div">
                        <span style="float: left;"><img ng-src="<?php echo base_url().UPLOAD_PROFILE ?>{{match.model.imageUrl}}" width="30" class="img-circle"></span>
                        <div class="search-name">
                            <span>{{match.model.name}}</span><br/>
                            <span ng-if="match.model.skillName.length">
                                <i class="skill">{{match.model.skillName}}</i>
                            </span>
                        </div>
                    </div>
                    <div style="padding-bottom:42px;" ng-if="match.model.length == 5">
                        <a href="<?php echo base_url(); ?>#/searchResult" style="float: left; padding: 18px 1px 5px 8px; color: black;"> 
                            Show More Results
                        </a>    
                    </div>
                </a>
</script> 

$scope.getMatchedUser = function(){
        $scope.searchValue = $scope.selected;
        $http({
            method : "POST",
            url : BASE_URL + 'getMatchedUser',
            data : $.param({"typeValue": $scope.selected}),
            headers : { 'Content-Type' : 'application/x-www-form-urlencoded'}
        }).success(function(data){ 
            if(data.status == "success"){
                $scope.searchMembers = data.searchMembers;
            }
        });
    };

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    ng-change 实际上是在ng-model 更新之前触发的。

    我发现的一个解决方案是简单地将 typeahead-wait-ms 属性(在可忽略不计的时间范围内)添加到您的 typeahead input 元素

    <input type="text" 
           placeholder="Search people here..." 
           ng-change="getMatchedUser();"  
           ng-model="selected" 
           data-typeahead="user as user.name for user in searchMembers | filter:$viewValue" 
           typeahead-wait-ms=10
           typeahead-on-select="onSelect($item, $model, $label)" 
           typeahead-template-url="customTemplate.html"  />
    

    这 10 毫秒的暂停应该足以让 ng-model 在触发 ng-change 事件之前更新。

    【讨论】:

    • typeahead-on-select="onSelect($item, $model, $label)" 不是基本上在模型实际发生变化时给我们提供了钩子吗?
    【解决方案2】:

    你应该直接使用搜索而不是使用 ng-change

    <input type="text" 
           placeholder="Search people here..." 
           ng-model="selected" 
           data-typeahead="user as user.name for user in getMatchedUser($viewValue) | filter:$viewValue" 
           typeahead-wait-ms=10
           typeahead-on-select="onSelect($item, $model, $label)" 
           typeahead-template-url="customTemplate.html"  />
    

    并调整您的搜索功能以使用参数 $scope.getMatchedUser = function(query){

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2017-07-14
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多