嗨我正在angularjs中开发web应用程序。我有一个表单,我已经把dropdownlistbox。下面是我的代码来填充dropdownboxlist数组。如何在Angularjs中的选择控件中设置默认选定值?
<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 = {}; $ scope.user.period = 1; $ scope.user = {} ;.我在最后还有$ scope.user = {}; –
删除。它会删除值 –
是啊,谢谢.... –