Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angularjs的选择控件中设置默认选定值?_Javascript_Angularjs - Fatal编程技术网

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

Javascript 如何在Angularjs的选择控件中设置默认选定值?,javascript,angularjs,Javascript,Angularjs,嗨,我正在用angularjs开发web应用程序。我有一张表格,我把dropdownlistbox放进去了。下面是我用数组填充dropdownboxlist的代码 <select class="with-icon" ng-model="user.period" name="period" id="period" ng-options="user.ID as user.period for user in periodList" required>

嗨,我正在用angularjs开发web应用程序。我有一张表格,我把dropdownlistbox放进去了。下面是我用数组填充dropdownboxlist的代码

<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>
我尝试默认设置第一个值,如下所示

$scope.user.period =  [{ ID: 1, period:'1 Month' }];
这种方法对我不起作用。 我尝试了以下方法$scope.user.period=1;即使这样对我也不起作用。
我尝试了$scope.user.period=$scope.periodList[1];这对我来说也不管用。有人能帮我设置正确的方法来默认选择第一个值吗。任何帮助都将不胜感激。谢谢。

试着这样设置:

var app=angular.modulemyApp,[]; app.controllermyCtrl,函数$scope{ $scope.user={}; $scope.periodList=[{ID:1,period:'1个月'},{ID:2,period:'2个月'},{ID:3,period:'3个月'},{ID:4,period:'4个月'},{ID:5,period:'5个月'},{ID:6,period:'6个月'}]; $scope.user.period=$scope.periodList[0].ID; }; {{'期间'}} {{user.period}
你可以用这样的东西

<select ng-init="somethingHere = periodList[0]"> ... </select>
$scope.user.period=1;可以正常工作。也许您忘记了先将用户声明为对象

 $scope.user = {};
 $scope.user.period =  1;
角度。模数PP,[] .controllerctrl,函数$scope{ $scope.periodList=[{ID:1,period:'1个月'},{ID:2,period:'2个月'},{ID:3,period:'3个月'},{ID:4,period:'4个月'},{ID:5,period:'5个月'},{ID:6,period:'6个月'}]; $scope.user={}; $scope.user.period=1; } {{'期间'}}
根据文档,您需要将ng模型指定为列表中的项目,用于创建您想要预选的选项。在本例中,$scope.periodList中的第一项


您可以在此处找到更多信息:

谢谢。没有运气问题是$scope.user={}$scope.user.period=1$scope.user={};。最后我又得到了$scope.user={};去掉那个。它将删除这些值
 $scope.user = {};
 $scope.user.period =  1;
<select class="with-icon"  ng-model="periodList[0]" 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>