ng-repeat没有显示数组中的值

时间:2016-03-14 19:36:57

标签: javascript angularjs

我正在使用ng-repeat指令,但它没有显示我的价值。这是我的代码

JS文件:

var artist = angular.module('artistApp',[]);
artist.controller('artistController',['$scope',function($scope){
$scope.author = 
{
    "name":"Barot Bellingham",
    "shortname":"Barot_Bellingham"
},
{
    "name":"Jonathan G. Ferrar II",
    "shortname":"Jonathan_Ferrar"
},
{
    "name":"Hillary Hewitt Goldwynn-Post",
    "shortname":"Hillary_Goldwynn"
}
}]);

HTML文件:

<head>
    <meta charset="utf-8">
    <title>Angular Demo</title>
    <script src="angular/angular.min.js"></script>
    <script src="js/controllers.js"></script>
</head>

<body>
    <div ng-controller="artistController">
        <ul>
            <li ng-repeat="item in author">
                {{item.name}}
            </li>
        </ul>
    </div>
</body>

我在这里做错了哪里的错误。

3 个答案:

答案 0 :(得分:2)

您缺少作者数组的左括号

$scope.author = 
{

需要:

$scope.author = 
[{

答案 1 :(得分:0)

将最后的'} 替换为']'

工作Plunker

更正了JSON:

  

[{           “名字”:“Barot Bellingham”,           “短名称”: “Barot_Bellingham”       },       {           “名字”:“Jonathan G. Ferrar II”,           “短名称”: “Jonathan_Ferrar”       },       {           “名字”:“Hillary Hewitt Goldwynn-Post”,           “短名称”: “Hillary_Goldwynn”       }       ]

答案 2 :(得分:0)

JS中的

$scope.author = 
[{
    "name":"Barot Bellingham",
    "shortname":"Barot_Bellingham"
},
{
    "name":"Jonathan G. Ferrar II",
    "shortname":"Jonathan_Ferrar"
},
{
    "name":"Hillary Hewitt Goldwynn-Post",
    "shortname":"Hillary_Goldwynn"
}];

<强> HTML

<li ng-repeat="item in author track by $index">
    {{item.name}}
</li>