最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular ng-options remove blank option and select the first option only - Stack Overflow

programmeradmin4浏览0评论

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?

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

My Snippet of code is here:

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

My controller has

$scope.userProfiles

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

Below is the screen shot:

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

Thanks, Ankit

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?

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

My Snippet of code is here:

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

My controller has

$scope.userProfiles

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

Below is the screen shot:

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

Thanks, Ankit

Share Improve this question asked May 7, 2015 at 14:49 Ankit TannaAnkit Tanna 1,8198 gold badges34 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

Do this :)

In your controller :

 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"
  };

In your page :

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

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

This has several solution. The following worked best for me. Just initialize the model with the first value. The ng-init="myItem = items[0].name" does the trick

<select ng-if="items" ng-init="myItem = items[0].name" ng-model="myItem">
     <option ng-repeat="item in items" ng-value="item.name">{{item.name}}</option>
</select>
发布评论

评论列表(0)

  1. 暂无评论