2014-11-03 51 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