在我的情况下,ng-repeat不适用于数据

时间:2014-04-15 05:01:47

标签: javascript angularjs

在我的index.html中我试过

<li ng-repeat="friend in user.relationship">{{friend.name}}</li>

这是我的json看起来像

var user = [
        {
        'uId': 1,
        'name': 'me',
        'relationship': 
            [
                {'uId':2,
                 'name': 'Jeremy',
                 'tabs':[{'tabId':1}],
                 'tasks':[{'name':'Im Jeremy Lin'}]
                }
            ]
        }
]

我甚至尝试过<li ng-repeat="friend in user[0].relationship">{{friend[0].name}}</li>

3 个答案:

答案 0 :(得分:1)

应该是

<li ng-repeat="friend in user[0].relationship">{{friend.name}}</li>

答案 1 :(得分:1)

以下代码可以成功显示“Jeremy”。我希望这会对你有所帮助。

的index.html

<body ng-controller="sample">

    <ul ng-repeat="friend in user[0].relationship">
        <li>{{friend.name}}</li>
    </ul>
 </body>

app.js

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


app.controller('sample', function($scope){

$scope.user = [{
    'uId': 1,
    'name': 'eldy',
    'relationship': [
            {'uId':2,
             'name': 'Jeremy',
             'tabs':[
                 {'tabId':1}
             ],
             'tasks':[
                 {'name':'Im Jeremy Lin'}
             ]
            }
        ]
    }]  

});

答案 2 :(得分:0)

尝试这一点,因为即使对于多个对象

,这也会很好
<ul ng-repeat="friend in user">
  <li ng-repeat="relation in friend.relationship">
    {{relation.name}}             
  </li>
</ul>

如果你有一个类似下面的json,你需要打印 Jeremy,Jasmin

请看一下 Working Example

$scope.user = [{
        'uId': 1,
            'name': 'eldy',
            'relationship': [{
            'uId': 2,
                'name': 'Jeremy',
                'tabs': [{
                'tabId': 1
            }],
                'tasks': [{
                'name': 'Im Jeremy Lin'
            }]
        }]
    }, {
        'uId': 2,
            'name': 'eldy1',
            'relationship': [{
            'uId': 3,
                'name': 'Jasmin',
                'tabs': [{
                'tabId': 2
            }],
                'tasks': [{
                'name': 'Im Jasmin Jacob'
            }]
        }]
    }];
相关问题