2013-07-24 168 views
1

鉴于我有angularjs选择框默认选择基于数据库ID

... 

<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,它在基于数组索引的基础上工作正常,但我需要在外键基础上做选择

我怎样才能默认选择S基于外键的选择框,而不是索引?

+0

其辱我的OpenID是又坏了工作,我不得不打开stackexchange占问这个问题,不知道任何其他方式,任何想法如何将我的openID帐户与这个帐户连接起来,这样我就可以同时使用认证(openID和stackexchange),如果其中一个不起作用,我可以使用其他方法。 – Developer

回答

4

你有如下修改HTML,然后它会与你的外键

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

$scope.customer = 9;