2013-07-24 153 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;