如何在角度预先搜索中使用ng-change

Kir*_*rti 3 javascript angularjs

我想在按键事件上搜索用户列表.我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;
            }
        });
    };
Run Code Online (Sandbox Code Playgroud)

sfl*_*che 7

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"  />
Run Code Online (Sandbox Code Playgroud)

这10ms的暂停应足以允许在触发事件ng-model之前更新ng-change.