AngularJS ng-repeat示例不起作用

时间:2013-10-30 16:40:48

标签: javascript angularjs ng-repeat

我目前正在尝试AngularJS书中的一个例子,但它不起作用。 我在Batarang说错了:

ypeError:无法读取未定义的属性“#”

这是HTML:

<body ng-app>

<h1>Countries</h1>

<ul ng-controller="WorldCtrl">
    <li ng-repeat="country in countries">
        {{country.name}} has population of {{country.population}}
    </li>
    <hr>
World's population: {{population}} millions
</ul>

</body>

我的js

var WorldCtrl = function ($scope) {
$scope.population = 7000;
$scope.countries [
    {name: 'France', population: 63.1},
    {name: 'United Kingdom', population: 61.8}
];
};

为什么这不起作用的任何想法? 感谢

3 个答案:

答案 0 :(得分:13)

您的代码中有拼写错误。您在=

之后错过了$scope.countries

使用

$scope.countries = [
    {name: 'France', population: 63.1},
    {name: 'United Kingdom', population: 61.8}
];

Working Demo

答案 1 :(得分:2)

您忘记了=

中的$scope.countries = [ {name: 'France', population: 63.1}, {name: 'United Kingdom', population: 61.8} ];

答案 2 :(得分:2)

看这里:

http://jsfiddle.net/konan/xaAxs/

function ctrl($scope) {

    $scope.population = 7000;
    $scope.countries = [
        {name: 'France', population: 63.1},
        {name: 'United Kingdom', population: 61.8}
    ];
};