我正在尝试使用ng-options
在我的选择框中选择默认值(从数据库)。
我的观点
<select class="form-control samlength modalinput"
ng-options="p.procid as p.procname for p in processes track by p.procid"
ng-model="p.procid">
<option value="">-- choose an option --</option>
</select>
其中p.procid
是从数据库接收的值。
我的数据
procid procname time
1 MyProcess 2018-05-30 13:34:54.097 3003162
3 Testing 2018-05-31 18:31:32.467 3003162
如果selected procid
为3,如何才能默认选中?
仅供参考-我已经尝试了在其他线程中给出的多个答案。我也尝试过ng-init
,但没有任何帮助。
发布于 2018-06-04 21:52:43
您可以将HTML保留为:
<select class="form-control samlength modalinput"
ng-options="p.procid as p.procname for p in processes track by p.procid"
ng-model="selectedProcess">
<option value="">-- choose an option --</option>
</select>
现在,如果我们需要选择数组中的特定对象。我们可以通过迭代数组并比较给定键值来做到这一点:
function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
如下所示调用此函数:
var index = functiontofindIndexByKeyValue($scope.processes, "procid", procid);
$scope.selectedProcess = $scope.processes[index];
alert(index);
希望它能起作用!
https://stackoverflow.com/questions/50644963
复制相似问题