0

我有一个对象

    $scope.colleges = [{"CollegeCode":"40","CollegeName":"College1"},
{"CollegeCode":"35","CollegeName":"College2"},
{"CollegeCode":"32","CollegeName":"College3"},
{"CollegeCode":"15","CollegeName":"College4"}]

我正在用它的内容填充一个选择元素

<select ng-model="collegeSelection" ng-options="c as c.CollegeName for c in colleges" name="selectCollege"></select>

它像这样呈现在页面上

<select class="ng-pristine ng-valid" name="selectCollege" ng-options="c as c.CollegeName for c in colleges" ng-model="collegeSelection" style="" selected="selected">
<option value="?" selected="selected"></option>
<option value="0">College1</option>
<option value="1">College2</option>
<option value="2">College3</option>
<option value="3">College4</option>
etc....

我还将这个选择绑定到collegeSelection,当一个项目被选中时,对象看起来像这样

$scope.collegeSelection = {"CollegeCode":"32","CollegeName":"College"}

当有人点击页面上的编辑时,我要默认选择的大学来自 userToUpdate 对象。

$scope.userToUpdate = {
            Id: 1,
            FirstName: 'John',
            LastName: 'Doe',
            CollegeCode: '35,
            CollegeName: 'College2',
            Active: true
        };

当一个编辑按钮和 userToUpdate 对象被填充时,我希望将 select 元素中的学院设置为用户对象中的学院。怎么可能做到这一点?仅供参考,呈现的选择中的值与 CollegeCode 不匹配。似乎角度使用了大学对象的索引。谢谢

4

1 回答 1

1

试试这个

<select ng-model="collegeSelection" ng-options="c.CollegeCode as c.CollegeName for c in colleges" name="selectCollege"></select>
    <span>{{collegeSelection}}</span>
    <input type="button" ng-click="switch()" name="switch" value="switch" />
    <script>
        var app = angular.module('plunker', []);

        app.controller('MainCtrl', function ($scope, $filter) {
            $scope.colleges = [{ "CollegeCode": "40", "CollegeName": "College1" },
                                { "CollegeCode": "35", "CollegeName": "College2" },
                                { "CollegeCode": "32", "CollegeName": "College3" },
                                { "CollegeCode": "15", "CollegeName": "College4" }
            ]

            $scope.switch = function () {
                $scope.collegeSelection = "32";
            };
        });
于 2013-07-15T13:43:36.633 回答