How to have a default option in Angular.js select box

后端 未结 23 1538
悲哀的现实
悲哀的现实 2020-11-22 06:04

I have searched Google and can\'t find anything on this.

I have this code.


                        
    
提交评论

  • 2020-11-22 06:07

    Use below code to populate selected option from your model.

    <select id="roomForListing" ng-model="selectedRoom.roomName" >
    
    <option ng-repeat="room in roomList" title="{{room.roomName}}" ng-selected="{{room.roomName == selectedRoom.roomName}}" value="{{room.roomName}}">{{room.roomName}}</option>
    
    </select>
    
    0 讨论(0)
  • 2020-11-22 06:09

    Using select with ngOptions and setting a default value:

    See the ngOptions documentation for more ngOptions usage examples.

    angular.module('defaultValueSelect', [])
     .controller('ExampleController', ['$scope', function($scope) {
       $scope.data = {
        availableOptions: [
          {id: '1', name: 'Option A'},
          {id: '2', name: 'Option B'},
          {id: '3', name: 'Option C'}
        ],
        selectedOption: {id: '2', name: 'Option B'} //This sets the default value of the select in the ui
        };
    }]);
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
    <body ng-app="defaultValueSelect">
      <div ng-controller="ExampleController">
      <form name="myForm">
        <label for="mySelect">Make a choice:</label>
        <select name="mySelect" id="mySelect"
          ng-options="option.name for option in data.availableOptions track by option.id"
          ng-model="data.selectedOption"></select>
      </form>
      <hr>
      <tt>option = {{data.selectedOption}}</tt><br/>
    </div>

    plnkr.co

    Official documentation about HTML SELECT element with angular data-binding.

    Binding select to a non-string value via ngModel parsing / formatting:

    (function(angular) {
      'use strict';
    angular.module('nonStringSelect', [])
      .run(function($rootScope) {
        $rootScope.model = { id: 2 };
      })
      .directive('convertToNumber', function() {
        return {
          require: 'ngModel',
          link: function(scope, element, attrs, ngModel) {
            ngModel.$parsers.push(function(val) {
              return parseInt(val, 10);
            });
            ngModel.$formatters.push(function(val) {
              return '' + val;
            });
          }
        };
      });
    })(window.angular);
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular.min.js"></script>
    <body ng-app="nonStringSelect">
      <select ng-model="model.id" convert-to-number>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    {{ model }}
    </body>

    plnkr.co

    Other example:

    angular.module('defaultValueSelect', [])
     .controller('ExampleController', ['$scope', function($scope) {
       $scope.availableOptions = [
         { name: 'Apple', value: 'apple' }, 
         { name: 'Banana', value: 'banana' }, 
         { name: 'Kiwi', value: 'kiwi' }
       ];
       $scope.data = {selectedOption : $scope.availableOptions[1].value};
    }]);
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
    <body ng-app="defaultValueSelect">
      <div ng-controller="ExampleController">
      <form name="myForm">
        <select ng-model="data.selectedOption" required ng-options="option.value as option.name for option in availableOptions"></select>
      </form>  
      </div>
    </body>

    jsfiddle

    0 讨论(0)
  • This working for me

    ng-selected="true" 
    
    0 讨论(0)
  • 2020-11-22 06:12

    I think the easiest way is

     ng-selected="$first"
    
    0 讨论(0)
  • 2020-11-22 06:14

    This worked for me.

    <select ng-model="somethingHere" ng-init="somethingHere='Cool'">
        <option value="Cool">Something Cool</option>
        <option value="Else">Something Else</option>
    </select>
    
    0 讨论(0)
  • 提交回复
    热议问题