angularjs - ng-repeat不会遍历hashmap

时间:2016-11-23 09:20:55

标签: javascript angularjs ng-repeat

我正在尝试迭代这样的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;
}

不起作用..

我得到空名单。

我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:4)

您正在初始化新数组而不是对象 它应该是$scope.data =[];而不是ng-repeat

您最好将li放在ul上,而不是<ul> <li ng-repeat="(name, person) in data"> name: {{name}} -------------------------- age: {{person.age}} hair: {{person.hair}} </li> </ul>

std::list<int> l;
l.push_back(4);
l.push_back(2);
l.push_back(1);
l.sort();