嗨,我正在 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] 删除。
我来说两句