我有一个代码,我希望使用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
中的初学者答案 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">
<强>样本强>
<!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;