1

鉴于我有

...

<select ng-model="customer" ng-options="c.name for c in customers">
    <option value="">-- chose customer --</option>
</select> 

....

在控制器中我有

$scope.customers = [
    {"id":4,"name":"aaaa","isActive":1,"isDeleted":0},
    {"id":5,"name":"testxyz","isActive":0,"isDeleted":0},
    {"id":9,"name":"bbb","isActive":1,"isDeleted":0},
    {"id":10,"name":"asdfa","isActive":0,"isDeleted":0},
    {"id":11,"name":"asdfa","isActive":0,"isDeleted":0}
        ];
if ($scope.$id != null)
{
    Message.query({id: $scope.$id}, function(data) {                    
    if (data[0].id) {
        $scope.name = data[0].name;
        $scope.fromName = data[0].fromName;
        $scope.subject = data[0].subject;

// this line does not work because data[0].custID is database
// customer ID Foreign Key linked with this record
//   {"id":4,"name":"aaaa","isActive":1,"isDeleted":0}     

        $scope.customer = data[0].custID;

// this works as it is based on index
//$scope.customer = $scope.customers[3]; 

    } else {
        alert('Request Failed: ' + data.msg);
    }
});

}

现在,当我尝试使用数据库中的预填充字段在编辑模式下编辑记录并打开视图时,我需要对 SELECT Box 进行默认选择,这在基于数组索引的情况下工作正常,但我需要在外键基础上进行选择

如何默认选择基于外键而不是索引的选择框?

4

1 回答 1

4

您必须如下修改您的 html,然后它将与您的外键一起使用

<select ng-model="customer" ng-options="c.id as c.name for c in customers">

 $scope.customer = 9;
于 2013-07-24T12:44:47.973 回答