4

I'm new to angular.js, but I cannot figure out how to make | filter:xxx work with data generated through $http service.

In the following code, I simply cannot get the filter to work with data generated by $http - it simply does nothing when I type inside input box. However, the filter DOES WORK if I hard code some data inside $scope function.

<div ng-controller="UserCtrl">

    <input ng-model="search">
    <ul>

        <li ng-repeat="user in users| filter:search">
            {{user.id}}
            {{user.firstname}}
            {{user.lastname}}
        </li>
    </ul>

</div>

<script>

function UserCtrl($scope, $http) {
$http.get('actions.php?action=get_user_list').success(function(data) {
    $scope.users = data;
  });

}

</script>
5
  • 2
    Figured out what it is - my php script was generating JSON array of objects with a key (i.e. 01 => array (...). Angular cannot filter this properly. The solution is to generate a list of objects without a key, for example: $output=array(array(data),array(data)) etc. Commented May 6, 2013 at 0:40
  • 1
    Maybe this will be useful for you: array_values strips keys, so array becomes simple 0-based, well, array. Commented May 11, 2013 at 12:02
  • Yup, an angular filter only works with arrays. Commented Aug 9, 2013 at 16:43
  • The filter in fact works with objects, not just arrays | filter: {name: search} Commented Aug 29, 2013 at 7:21
  • @pvukovic You should post your comment as an answer! Commented Feb 27, 2015 at 21:37

2 Answers 2

0

This can serve you:

<li ng-repeat="user in users| filter:{firstname : search}">
Sign up to request clarification or add additional context in comments.

Comments

0

Hey test with below codes

<div ng-controller="UserCtrl>
    <input ng-model="search">
    <ul>
        <li ng-repeat="user in users | filter:search.$">
            {{user.id}}
            {{usesr.firstname}}
            {{user.lastname}}
    </ul>
</div>

<script>

var app = angular.module('Your Module Name' ,[]);

app.controller('UserCtrl', function($scope, $http){
    $http.get('actions.php?action=get_user_list').then(function(response){
        $scope.users = response.data;
    });
});

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.