无法在angularjs中循环值

时间:2018-01-21 08:48:57

标签: javascript html angularjs

我有一个代码,我希望使用ng-repeat和ng-init以json格式循环遍历数组。

但代码不起作用。

以下是以下代码。

<!DOCTYPE html>
<html ng-app="">
<head>
    <title></title>
</head>
<body>
    <h1>
        ng - repeat
    </h1>
    <hr />
    <div ng-init="myFavLan=[{name:'PHP',extension:'.php'},
                            {name:'Javascript',extension:'.js'},
                            {name:'HTML',extension:'.html'}
                           ]">
        <p ng-repeat="language in myFavlan">
            Name : {{ language.name }} <br />
            Extension : {{ language.extension }}
        </p>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>
</html>

请注意,我是angularjs

中的初学者

2 个答案:

答案 0 :(得分:3)

Javascript区分大小写,因此ng-repeat中的变量myFavlan未定义。只需改变

<p ng-repeat="language in myFavlan">

<p ng-repeat="language in myFavLan">

您还可以更改ng-init变量。

答案 1 :(得分:2)

应该 myFavLan 更改

<p ng-repeat="language in myFavlan">

<p ng-repeat="language in myFavLan">

<强>样本

&#13;
&#13;
<!DOCTYPE html>
<html ng-app="">
<head>
    <title></title>
</head>
<body>
    <h1>
        ng - repeat
    </h1>
    <hr />
    <div ng-init="myFavLan=[{name:'PHP',extension:'.php'},
                            {name:'Javascript',extension:'.js'},
                            {name:'HTML',extension:'.html'}
                           ]">
        <p ng-repeat="language in myFavLan">
            Name : {{ language.name }} <br />
            Extension : {{ language.extension }}
        </p>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>
&#13;
&#13;
&#13;