角度 <select> ng 选项 选择值未正确设置



我有这个伪代码angularjs应用程序。你可以为每个人选择一个爱好,保存它并再次加载它。然而,当我加载数据时,选择框没有选择正确的选项。person.hobby存在并且是正确的对象,但是看起来ng-model没有正确设置。我错过什么了吗?

<div ng-repeat="person in people">
    <p>{{ person.fullname }}</p>
    <select ng-model="person.hobby"
            ng-options="h.hobby_name group by h.category for h in hobbies">
    </select>
</div>
<script>
     //... controller...
     $http.get('/gethobbies/').succes(data) {
        hobbies = data;
     };
    $http.get('/getpeople/').succes(data) {
        people = data;
        // looks like this:  [{'fullname': 'bill gates', 'hobby': {'hobby_name': 'programming', 'category': 'computers'}, ...]
    };
</script>

ng-model需要设置为与您想要选择的ng-options数组中的对象完全相同的对象。Angular使用对象引用来确定哪一个应该是活动的,所以拥有一个与"爱好"中的一个对象具有相同"hobby_name"的"爱好对象"是不够的。必须是对同一对象的引用。

查看select的详细信息

最新更新