JRoller JRoller - 5 months ago 12
jQuery Question

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

I'm new to Angular and trying to create a filtering mechanism that fires a search when typing a letter, looking it up the db and returning results.

The results are returning ok from the DB, but the issue i'm having is when i'm filling the scope array with the results and ng-repeat doesn't fire.

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>





I'll be glad if anyone could help.

Answer

Remove $scope from the repeat in the view, use only results

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