angularjs - ng-repeat 不会遍历 hashmap
angularjs - ng-repeat doesn't iterate over hashmap
我正在尝试像这样迭代 hashmap:
index.html
<div ng-app>
<div ng-controller="ClickToEditCtrl">
<p>Your Data:</p>
<ul ng-repeat="(name, person) in data">
name: {{name}}
--------------------------
age: {{person.age}}
hair: {person.hair}}
</ul>
</div>
</div>
在我的 ClickToEditCtrl.js
function ClickToEditCtrl($scope) {
$scope.data =[];
$scope.data["Mike"] = new person (11,"blonde");
$scope.data["Dan"] = new person (22, "redhead");
}
function person(age, hair){
this.age = age;
this.hair = hair;
}
那行不通..
我得到的是空列表。
我做错了什么?
谢谢!
您正在初始化新数组而不是对象。
它应该是 $scope.data = {};
而不是 $scope.data =[];
你最好把 ng-repeat
放在 li
而不是 ul
<ul>
<li ng-repeat="(name, person) in data">
name: {{name}}
--------------------------
age: {{person.age}}
hair: {{person.hair}}
</li>
</ul>
我正在尝试像这样迭代 hashmap:
index.html
<div ng-app>
<div ng-controller="ClickToEditCtrl">
<p>Your Data:</p>
<ul ng-repeat="(name, person) in data">
name: {{name}}
--------------------------
age: {{person.age}}
hair: {person.hair}}
</ul>
</div>
</div>
在我的 ClickToEditCtrl.js
function ClickToEditCtrl($scope) {
$scope.data =[];
$scope.data["Mike"] = new person (11,"blonde");
$scope.data["Dan"] = new person (22, "redhead");
}
function person(age, hair){
this.age = age;
this.hair = hair;
}
那行不通..
我得到的是空列表。
我做错了什么?
谢谢!
您正在初始化新数组而不是对象。
它应该是 $scope.data = {};
而不是 $scope.data =[];
你最好把 ng-repeat
放在 li
而不是 ul
<ul>
<li ng-repeat="(name, person) in data">
name: {{name}}
--------------------------
age: {{person.age}}
hair: {{person.hair}}
</li>
</ul>