Angular ng-options 删除空白选项并仅选择第一个选项 [英] Angular ng-options remove blank option and select the first option only

查看:30
本文介绍了Angular ng-options 删除空白选项并仅选择第一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AngularJS 从控制器中的对象数组动态填充我的选择选项内容.我的对象有一个名为 userProfileName 的属性.

I am using AngularJS to populate my select options content dynamically from an array of objects in my controller. My objects has a property named userProfileName.

如何删除我在顶部看到的空白选项?

How would it be possible to remove the blank option that I am getting at the top?

此外,我想默认选择第一个配置文件配置文件 1.这怎么办?

Also, I would want to select the first profile Profile 1 by default. How can this be done?

我的代码片段在这里:

<select id="requestorSite" 
                             ng-model="selectedUserProfile"
                             ng-options="userProfile.userProfileName for userProfile in userProfiles"
                             ng-selected=""
                             class="form-control displayInlineBlock width40per marginLeft15">
                            </select>

我的控制器有

$scope.userProfiles

作为对象数组,每个对象都有userProfileName属性.

As the array of object and each object has userProfileName attribute.

以下是截图:

我想删除顶部的空白选项,并默认选择配置文件 1.

I would like to remove the blank option at the top and also have by default Profile 1 selected.

谢谢,安吉特

推荐答案

这样做 :)

在您的控制器中:

 function myCtrl ($scope) {
   $scope.userProfiles = [
     {id: 10, name: 'Carton'},
     {id: 27, name: 'Bernard'},
     {id: 39, name: 'Julie'},
  ];

   $scope.selectedUserProfile= $scope.userProfiles[0]; // Set by default the   value "carton"
  };

在您的页面中:

      <select  id="requestorSite" ng-model="selectedUserProfile" ng-options="userProfile as userProfile.name for userProfile in userProfiles">
            </select>

CodePen:http://codepen.io/anon/pen/qdOGVB

这篇关于Angular ng-options 删除空白选项并仅选择第一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆