13

JS:

function UserController($scope, User, Group){

    $scope.users = User.query();

    $scope.isNewUser = function(){

            var len = $scope.users.lenght;
            console.log(len); //undefined

            for(var i = 0; i < len; i++){
                if($scope.users[i].created_at == null){
                    return false;
                }
            }

            return true;
        }
    }
}

In frontend I have a list of users that are rendered well. However when I retrieve the length I get undefined. Why? I need the length of users to loop through the array of objects.

1
  • when i use this in view {{users.length}} and if have data than ok but if null than it shows random/garbage value .how to fix it Commented Jun 10, 2017 at 6:33

3 Answers 3

118

use:

$scope.users.length;

Instead of:

$scope.users.lenght;

And next time "spell-check" your code.

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

2 Comments

welcome to the magic world of "everything-is-a-runtime-error" languages. No more compile errors baby!
I avoid this by using jslint/jshint, which is built into Codekit and tells me if I have any errors each time I save the JS file. The console does not flag these errors, so these features are vital to efficient coding. Otherwise you may spend hours trying to fix a typo somewhere.
0

Make it like this:

$scope.users.data.length;

Comments

-1
var leg= $scope.name.length;
$log.info(leg);

1 Comment

Would be nice to add a few words on what exactly you are suggesting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.