Ng-repeat不会在$scope更改后第二次触发

ng-repeat doesn't fire the second time after $scope changes

本文关键字:第二次 scope Ng-repeat      更新时间:2023-09-26

我是Angular的新手,正在尝试创建一个过滤机制,在输入字母时触发搜索,在数据库中查找并返回结果。

结果从DB返回ok,但我遇到的问题是,当我用结果填充范围数组时,ng-repeat不会触发。

my HTML and JS:

    app.controller("friendsController",function($scope,$http){
        $scope.results =[];
        $scope.onTextChange = function (){
            var results = [];
            var text = $scope.friendsLookupText;
            $http({
                url: 'utilities/getFriendsByValue.php',
                method: 'POST',
                data: {textValue: text}
            }).success(function(data) {
                if(data=="none")
                {
                    $scope.results = results;
                }
                else
                {
                    var stringResult = JSON.stringify(data);
                    var parsedResult = JSON.parse(stringResult);
                    $scope.results = parsedResult;
                }
            });
        };
    });
<div class="container-fluid" ng-controller="friendsController as friendsCtrl">
  <input type="text" id="friendsLookupText" ng-model="friendsLookupText" ng-change="onTextChange()">
   <ul class="list-group">
     <div class="" ng-repeat="result in $scope.results">
         <li class="list-group-item">{{result.username}}</li>
     </div>
   </ul>
</div>

如果有人能帮忙我将很高兴

删除视图中repeat的$scope,只使用results

<div class="" ng-repeat="result in results">
     <li class="list-group-item">{{result.username}}</li>
 </div>