2014-11-03 73 views
1

我有我的angularjs視圖,我必須填充我的選擇框保存在數據庫中的值,並允許選擇其他選項。我嘗試過這樣:選擇 - 編輯帖子時填充

<div class="form-group"> 
<label class="control-label" for="status">Status zaposlenika</label> 
<div class="controls"> 
<select required name="status" class="form-control" ng-model="employee.status" ng-options="statusType.name for statusType in statusTypes" > 
</select> 
</div> 

但我的價值沒有填充我的看法。 ({{employee.status}} - >「test」)

$scope.statusTypes = [ 

    { 
     name: 'test' 
    }, 

    { 
     name:'Test1' 
    }, 

    { 

     name: 'Test2' 
    }, 

    { 

     name:'Test3' 
    } 
]; 

我該怎麼做?

編輯

我的模型employee.status填充了值 「測試」。但我的選擇框不是。其他值被列爲選擇項目。如何設置保存在我的數據庫中的默認值,以便在我的選擇框中預先選擇。

+0

你不必在你'statusTypes'值'test',所以它不能被選擇。 – dfsq 2014-11-03 12:40:23

+0

模型employee.status填充從數據庫獲取數據使用服務和$ resorce – Sysrq147 2014-11-03 12:40:46

+0

哦,很抱歉,價值是我的壞。但那不起作用。 – Sysrq147 2014-11-03 12:41:47

回答

1

您的模型employee.name是一個字符串,selectbox綁定到類似於{name: "Test1"}的對象。所以如果你想從statusTypes中選擇選項,你必須在對象數組中找到相應的對象。

$scope.statusTypes = [ 
    {name: 'Test1'}, 
    {name: 'Test2'}, 
    {name: 'Test3'} 
]; 

var selectedStatus = $scope.statusTypes.filter(function(type) { 
    return type.name = 'Test2'; 
})[0]; 

$scope.employee = { 
    status: selectedStatus 
}; 

所以你必須做出employee.statusstatusTypes陣列的對象之一。

或者另一種選擇是繼續使用字符串employee.status和改變ngOptions綁定到一個字符串,而不是對象:

ng-options="statusType.name as statusType.name for statusType in statusTypes" 
+0

謝謝你打開我的愚蠢的眼睛:) – Sysrq147 2014-11-03 12:55:29

+1

沒問題,ngOptions是一個棘手的指令,我知道.. – dfsq 2014-11-03 12:56:39

+0

一個非常奇怪的事情發生時,當我上傳我的價值觀,有時它不會工作:(有時價值是綁定到該字符串有時它不是。我使用secound選項。 – Sysrq147 2014-11-03 13:39:01