dongyunshan4066 2014-01-17 03:38
浏览 288

Angularjs:显示对象数组

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":"john.doe@email.com","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.


I tried what was suggested but to no avail. 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":"john.doe@email.com","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.

  • 写回答

3条回答 默认 最新

  • duanhuan5409 2014-01-17 05:13
    关注

    Try this.

    Hope this one will help

         <ul ng-repeat="(key,value) in user[0]"> 
                <li>{{key}} : {{value}}</li>      
         </ul>  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值