2015-05-07 121 views
3

我正在使用AngularJS从我的控制器中的对象数组中动态填充我的选择选项内容。我的对象有一个名为userProfileName的属性。Angular ng-options删除空白选项并仅选择第一个选项

怎么可能删除我在顶部的空白选项?

此外,我想要选择第一个配置文件配置文件1默认情况下。如何才能做到这一点?

我的代码段是在这里:

<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 

作为对象的阵列,并且每个对象具有用户配置文件名属性。

下面是截图: enter image description here

我想在顶部去除空白选项,默认情况下也简介1选择有。

感谢, ANKIT

回答

10

做到这一点:)

在你的控制器:

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

+0

这解决了我选择的问题:) –

+0

现在我正在寻找空白选项问题 –

+1

空白选项的问题?这段代码把值放在select中,但是他也放了一个默认值,这样空白的问题就可以解决了。请告诉我们你的代码。 – carton

相关问题