![](/img/trans.png)
[英]How to set the value of an option when using ng-options in controller?
[英]how to set the value of an option when using ng-options in a select with angularjs
我有一个问题,我需要发布表的id字段来更新db中记录的外键,但是我正在使用ng-options
属性填充select的选项。 我在线搜索,发现要设置值,我必须使用ng-model="dataSet.desiredColumnName"
将值更改为我选择的其他任何列
所以我有这段HTML供选择;
<select id="affiliation" ng-model="affiliationTable.affiliation_id" ng-change="alert(jQuery('#affiliation option:selected').val());" name="affiliation" ng-options="row.desc for row in affiliationTable"></select>
问题是,我想根据单击html表中的哪一行来选择特定的选项。 表格是这样定义的;
<tr ng-repeat="row in tableData">
<!--{{row.name}}-->
<td>
<a href='#' ng-click="invokeModal(row);" id="{{row.id}}">{{row.name}}</a>
</td>
<td>{{row.active}}</td>
<td>{{row.end_date}}</td>
<td>{{row.start_date}}</td>
</tr>
invokeModal使用选定的行来确定在select#affiliation中选择哪个选项;
$scope.invokeModal = function (row) { //memberDescription
if(row == undefined){
//blah blah ...
}else{
//blah blah ...
angular.forEach($scope.affiliationTable, function (affiliation) {
if (affiliation.id == row.affiliation_id) {
$scope.affiliationTable.affiliation_id = affiliation.affiliation_id;
}
});
}
//invokes the modal window that select#affiliation is held within
jQuery("#mem").modal('show');
};
我的问题是,似乎在选择的开头添加了一个空选项,并使该选项处于选中状态。 。 .wtf?
我认为您需要更正ng-option表达式,应该
row.id as row.desc for row in affiliationTable
当您从代码中分配模型属性时,select上的ng-model值应为相同类型。 您正在分配表的id属性,因此在选择模型时应使用当前设置为完整行对象的id
属性更新模型。
ng-options =“值作为列表中对象的groupName的标签组”
value可以是任何唯一的标识符,label可以是要显示的任何文本,groupname是根据需要对项目进行分组的值,而object是要在其上进行遍历的列表的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.