Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For the life of me I cannot extract and print out the data in my array. I've generated json in PHP and I've wired up Angularjs to retrieve this data. The Angularjs development tool within Chrome is telling me that I have a variable "user" that is an array of objects (see below) when I run the code. I made user a rootScope variable "$rootScope.user = data;" so I can get at it. I'm a newb so none of this is probably best practice.

{ 
    user : [{"user_id":"1","firstname":"John","lastname":"Doe","email":"[email protected]","age":"29","height":"74.5","weight":"200","gender":"M","birthdate":"2013-12-31","username":"john.doe","hash":"$2a$10$MECYLWuu6AwRxzBF\/CUys.AuqVm7Lf4BYRGezeusvLe6wNTkU3FEy"}]
} 

This is the ng-repeat I use on the same page that shows the above "user" is within its scope:

<ul id="biometrics" ng-repeat="info in user">
      <li>Age: {{info.age}}</li>
</ul>

What am I doing wrong? Perhaps what am I NOT doing wrong. I'm new to Angularjs to say the least. Thanks in advance for any help that can be given.


I tried what was suggested but to no avail. Thanks very much BTW. I just want to try and clarify my code/situation just to be sure I didn't screw up. I think I may have been unclear as this is also my first time posting on StackOverflow.

This is literally what my login.php file returns:

[{"user_id":"1","firstname":"John","lastname":"Doe","email":"[email protected]","age":"29","height":"74.5","weight":"200","gender":"M","birthdate":"2013-12-31","photoURL":"john.doe.jpg","username":"john.doe","hash":"$2a$10$MECYLWuu6AwRxzBF\/CUys.AuqVm7Lf4BYRGezeusvLe6wNTkU3FEy"}]

This is my login controller: app.controller("LoginController", ['$scope', '$rootScope', '$http', '$location', function($scope, $rootScope, $http, $location) { $scope.errorMessage = "Wrong password or username";

$scope.login = function() {

    $http({
        url: "./login.php",
            method: "POST",
            data: { cache: false, username: $scope.username, password: $scope.password },
            headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} 
        }).success(function(data, status, headers, config) {
            $rootScope.user = data;
            $location.path('/dashboard');
        }).error(function(data, status, headers, config) {
            $scope.statusMessage = status;
            alert($scope.errorMessage);
    });
}

}]);

This is in my view html:

<ul id="biometrics" ng-repeat="info in user">
            <li>Age: {{info}}</li>
        </ul>

I get nothing when I try this and I tried data.user and got nothing. Sorry if I'm being "stupid" about this...classic newb. :P

share|improve this question
    
It seems $rootScope.user = data should be $rootScope.user = data.user. Show the ng-repeat code too? –  Chandermani Jan 17 at 3:52
add comment

3 Answers

Here is a plunk! You were close! Welcome to angular. Its great! :P You were just off in accessing the .user property of your data.

You mention best practices and one thing to remember when writing templates is that, like writing a javascript function with local (scope) variables:

function func(){
    var myLocalVar = 'Data!';
    //Accessing it!
    myLocalVar = 'DataAccessed';
}

your templates 'local (scope) variables' are whatever is defined on $scope

Accessing them in the template is the same concept too. So generally defining things on $rootScope is akin to global variables (in lookup, not safety) and not necessarily a great idea unless needed.

share|improve this answer
add comment

Try this.

Hope this one will help

     <ul ng-repeat="(key,value) in user[0]"> 
            <li>{{key}} : {{value}}</li>      
     </ul>  
share|improve this answer
add comment

your data is an array with 1 element (an object literal) and needs to be referenced as an array.

1 quick fix you can do in your $http.success callback:

$rootScope.user = data[0]; /* <-- notice the "[0]" to reference it as an array */

Now you can reference your data in the html like this:

<ul id="biometrics" ng-repeat="info in user">
    <li>Age: {{info.age}}</li>
</ul>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.