angularjs根据数据库ID选择框默认选择

时间:2013-07-24 11:42:20

标签: javascript angularjs

在视图中我有

...

<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进行默认选择,它在基于数组索引时工作正常,但我需要在外部选择关键基地

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

1 个答案:

答案 0 :(得分:4)

你必须修改你的html如下,然后它将使用你的外键

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

 $scope.customer = 9;
相关问题