0

I'm using AngularJS and I'm displaying a list with ng-repeat. What I'm trying to achieve is to filter the list and only show the items that contain the value that the user writes in the input.

I set it up like the example found here but had no success.

HTML:

<div ng-if="result.length > 0">
    <input type="text" class="pl-4" ng-model="searchText" placeholder="Search" />

    <div class="mt-4 text-left animationIf" ng-repeat="item in result | filter: searchText">
        <div>
            <span class="text-danger">Name: </span>
            <span class="m-0" ng-bind="item.name"></span>
        </div>
        <div>
            <span class="text-danger">Country: </span>
            <span class="m-0" ng-bind="item.country"></span>
        </div>
        <div>
            <span class="text-danger">Born: </span>
            <span class="m-0" ng-bind="item.born"></span>
        </div>
        <div>
            <span class="text-danger">Surnname: </span>
            <span class="m-0" ng-bind="item.surname"></span>
        </div>
    </div>
</div>

An example of the result object would be like this:

$scope.result = [{
    name: "John",
    country: "California ",
    born: "283 AC",
    surname: "Snow"
},{
    name: "Michael",
    country: "US",
    born: "1958",
    surname: "Jackson"
}];

If I give at ng-init="$scope.result = 'John'" the filter works and displays only items that contain 'John' but if I later change the value o the input to 'Michael', it will do nothing. No filter applies.

What am I doing wrong here?

iSpithash
  • 56
  • 9
  • This issue with primitives can be easily avoided by following the "best practice" of always have a `'.'` in your ng-models – georgeawg Nov 04 '18 at 16:51

4 Answers4

1

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.result = [{
    name: "John",
    country: "California ",
    born: "283 AC",
    surname: "Snow"
},{
    name: "Michael",
    country: "US",
    born: "1958",
    surname: "Jackson"
}];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<input type="text" class="pl-4" ng-model="searchText" placeholder="Search" />
<body ng-app="testApp" ng-controller="testCtrl">
 <div class="mt-4 text-left animationIf" ng-repeat="item in result | filter: searchText">
    <div>
        <span class="text-danger">Name: </span>
        <span class="m-0" ng-bind="item.name"></span>
    </div>
    <div>
        <span class="text-danger">Country: </span>
        <span class="m-0" ng-bind="item.country"></span>
    </div>
    <div>
        <span class="text-danger">Born: </span>
        <span class="m-0" ng-bind="item.born"></span>
    </div>
    <div>
        <span class="text-danger">Surnname: </span>
        <span class="m-0" ng-bind="item.surname"></span>
    </div>
</div>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

Your code works as it is.. The issue occurs in the NgInit where you setting the scope object result to just "John" which is not an array and won't work out-of-the-box with NgRepeat.

So just dont set the $scope.result = 'John' and everything works fine

angular.module("app",[])
.controller("myCtrl", function($scope){
$scope.result = [{
    name: "John",
    country: "California ",
    born: "283 AC",
    surname: "Snow"
},{
    name: "Michael",
    country: "US",
    born: "1958",
    surname: "Jackson"
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>

<div ng-app="app" ng-controller="myCtrl">
<input type="text" class="pl-4" ng-model="searchText" placeholder="Search" />

<div class="mt-4 text-left animationIf" ng-repeat="item in result | filter: searchText">
    <div>
        <span class="text-danger">Name: </span>
        <span class="m-0" ng-bind="item.name"></span>
    </div>
    <div>
        <span class="text-danger">Country: </span>
        <span class="m-0" ng-bind="item.country"></span>
    </div>
    <div>
        <span class="text-danger">Born: </span>
        <span class="m-0" ng-bind="item.born"></span>
    </div>
    <div>
        <span class="text-danger">Surnname: </span>
        <span class="m-0" ng-bind="item.surname"></span>
    </div>
</div>
</div>
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
  • So strange... In the real application, I have over 1000+ items. You think this could be the problem? – iSpithash Nov 04 '18 at 12:27
  • @iSpithash updated the answer.. And no, the filter should work the same regardless of how many items in the array. – Marcus Höglund Nov 04 '18 at 12:28
  • Oh I see what's going on. I have an outer div that i didn't include in the question. This div has an ng-if="result.length > 0" to only show when results are ready. this makes the problem. – iSpithash Nov 04 '18 at 13:04
0

I don't see any problem with your code. But do not use ng-init command, like you mentoned:

ng-init="$scope.result = 'John'"

this command is clearing the array you have created in controller.

Ashish Diwakar
  • 61
  • 2
  • 12
  • Yeah, I know. I tried ng-init just to check if it would work, the normal application doesn't have ng-init at all. – iSpithash Nov 04 '18 at 12:28
0

I had an outer DIV which had ng-if="result.length > 0" so that it shows only when I populate the results. This made the input not working at all.

Removing the ng-if and putting ng-show instead, solved my problem.

iSpithash
  • 56
  • 9