如何在 Angularjs 的选择控件中设置默认选定值?

尼兰詹·戈德博尔

嗨,我正在 angularjs 中开发 Web 应用程序。我有一个表格,我已经放了下拉列表框。下面是我用数组填充下拉框列表的代码。

<select class="with-icon"  ng-model="user.period" name="period" id="period" ng-options="user.ID as user.period for user in periodList" required>
                                                    <option value="" label="{{ 'Period' | translate }}">{{ 'Period' | translate }}</option>
                                                </select>

下面是我的JS代码。

 $scope.periodList = [{ ID: 1, period: '1 Month' }, { ID: 2, period: '2 Month' }, { ID: 3, period: '3 Month' }, { ID: 4, period: '4 Month' }, { ID: 5, period: '5 Month' }, { ID: 6, period: '6 Month' }];

我尝试默认设置第一个值,如下所示。

$scope.user.period =  [{ ID: 1, period:'1 Month' }];

这种方法对我不起作用。我尝试了以下方法,$scope.user.period=1;即使这对我不起作用。我试过了$scope.user.period = $scope.periodList[1];,这也对我有用。有人可以帮我提出正确的方法来默认选择第一个值。任何帮助,将不胜感激。谢谢你。

萨奇拉·拉纳瓦卡

它与$scope.user.period = 1;. 也许您忘记先将用户声明为对象

 $scope.user = {};
 $scope.user.period =  1;

angular.module("app",[])
.controller("ctrl",function($scope){
 $scope.periodList = [{ ID: 1, period: '1 Month' }, { ID: 2, period: '2 Month' }, { ID: 3, period: '3 Month' }, { ID: 4, period: '4 Month' }, { ID: 5, period: '5 Month' }, { ID: 6, period: '6 Month' }];
$scope.user = {};
$scope.user.period =  1;

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <select class="with-icon"  ng-model="user.period" name="period" id="period" ng-options="user.ID as user.period for user in periodList" required>
                                                    <option value="" label="{{ 'Period' }}">{{ 'Period'  }}</option>
                                                </select>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在angularjs中设置选择框的默认值

如何在AngularJS的下拉框中设置选择默认值?

如何在angularjs中为select设置默认值

如何在angularjs的对象中选择内容中设置值

如何在角材料选择列表中设置默认选定值?

AngularJS选择框设置文档就绪时的默认选定值

如何在Angularjs上从int设置默认值<select>

如何在Angularjs中获取选定的optgroup值?

如何在ng-repeat中的angularjs中的文本框中设置默认值

如何在 AngularJS 中获取选定的记录?

如何设置默认十进制值以在angularjs中输入

如何在选择控件中设置初始值?

如何在angularjs中传递选择选项值

如何从angularjs的textarea中获取选定的值

如何在 Angularjs 的下拉框列表中设置值?

如何在AngularJS中设置value属性

如何在AngularJS中设置登录页面

如何在插入到选择中设置默认值?

如何在Angular Chosen中设置默认选择的值?

如何在指令中设置选择框的默认值

如何在angularjs中重置禁用值

如何在angularjs中获取指令的值

如何在angularjs中删除localstorage值

如何在Angularjs中访问表单值?

在angularjs ui-select指令更新后,如何在搜索字段中默认设置更新值?

如何通过AngularJS使用按钮从选择/下拉列表中获取选定的值?

如何在Ortuman / SwiftForms中的分段控件中设置默认选择

如何在EnumDropDownListFor中设置选定的值?

如何在选定的列中设置值?