0

I'm learning about AngularJS and I have an error in Chrome console when I do this:

            <input ng-model="search" />
            <ul>
                <li ng-repeat="prof in profesores | filter:search">
                    {{prof.nombre}} - {{prof.telefono}}
                </li>
            </ul>

with this data "profesores":

    {
      "profesores": [
          {
            "id": 0,
            "sexo": "hombre",
            "nombre": "Kari",
            "snombre": "Carr",
            "telefono": "(826) 453-3497",
            "celular": "(801) 9175-8136"
          },
          {
            "id": 1,
            "sexo": "mujer",
            "nombre": "Tameka",
            "snombre": "Gamble",
            "telefono": "(824) 438-2499",
            "celular": "(801) 8595-8337"
          }    ]
}

The error is this: Error: [filter:notarray]

The code works correctly (filter the items with string in input) but I have the error in console. What am I doing wrong?

5
  • this is because you can not use profesores directly like this Commented Feb 25, 2016 at 11:10
  • Can you please tell, where are you initialising the array ? Commented Feb 25, 2016 at 11:11
  • you should initialize profesores like this var profesores = [{ id:0,... },{ id:1,... }] Commented Feb 25, 2016 at 11:17
  • Yes, @Codetoend this is my code for initialising the "profesores" var: $http.get('json/profesores.json') .success(function(data){ $scope.profesores = data.profesores; }); Commented Feb 25, 2016 at 12:48
  • @monikaja , as you are initialising it in a promise so until the promise is not returned it just a null object or not defined , hence the error . You can initialize your array with empty array when js loads to avoid this error. Follow my answer below. Commented Feb 25, 2016 at 15:26

3 Answers 3

1

Here ng-repeat="prof in profesores | search" --> wrong

ng-repeat="prof in profesores |filter: search" --> right

var myApp = angular.module('myApp',[]);

myApp.controller('samplecontroller',  function($scope) {
$scope.profesores = [
      {
        "id": 0,
        "sexo": "hombre",
        "nombre": "Kari",
        "snombre": "Carr",
        "telefono": "(826) 453-3497",
        "celular": "(801) 9175-8136"
      },
      {
        "id": 1,
        "sexo": "mujer",
        "nombre": "Tameka",
        "snombre": "Gamble",
        "telefono": "(824) 438-2499",
        "celular": "(801) 8595-8337"
      }
]; 
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html >

<head>
   
</head>

<body ng-app="myApp">

<div ng-controller="samplecontroller">
 <input ng-model="search" />
            <ul>
                <li ng-repeat="prof in profesores |filter: search">
                    {{prof.nombre}} - {{prof.telefono}}
                </li>
            </ul>


</div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry Suresh, I've made a mistake. It's filter:search in my code too. So that isn't the reason of console error.
0

Initialiae it with an empty array like this when js loads:

$scope.profesores = []

Then you can define the variable again in your promise :

$http.get('json/profesores.json') .success(function(data){ $scope.profesores = data.profesores; }); 

3 Comments

Thank you so much @Codetoend !! I had initialized the var profesores like that: $scope.profesores = {}; Any reason for the error? Something about the definition of ng-repeat or filter definitions?
This is to initialise an object: $scope.profesores = {}; while for array you use square brackets as mentioned above and ng-repeat expects an array to iterate and throws an error when provided with anything else. Therefore when promise is returned it gets re-initialised with an array and you get your desired results , thats the power of angular live reloading with $digest .
If it solved your problem mark this as correct answer. Thanks !!
0

Until the data is loaded from the server your profesores variable is undefined and will throw the error. Initialize it with an empty array at the beginning o your controller:

$scope.profesores = [];

After that you can request the data and fill the array:

$http.get('json/profesores.json').success(function(response){ 
    $scope.profesores = response.profesores; 
}); 

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.