how to use ng-option to set default value of select element

前端 未结 12 661
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 11:07

I\'ve seen the documentation of the Angular select directive here: http://docs.angularjs.org/api/ng.directive:select. I can\'t figure how to set the default value. This is

相关标签:
12条回答
  • 2020-11-22 12:00

    I struggled with this for a couple of hours, so I would like to add some clarifications for it, all the examples noted here, refers to cases where the data is loaded from the script itself, not something coming from a service or a database, so I would like to provide my experience for anyone having the same problem as I did.

    Normally you save only the id of the desired option in your database, so... let's show it

    service.js

    myApp.factory('Models', function($http) {
    var models = {};
    models.allModels = function(options) {
        return $http.post(url_service, {options: options});
    };
    
    return models;
    });
    

    controller.js

    myApp.controller('exampleController', function($scope, Models) {
    $scope.mainObj={id_main: 1, id_model: 101};
    $scope.selected_model = $scope.mainObj.id_model;
    Models.allModels({}).success(function(data) {
        $scope.models = data; 
    });
    });
    

    Finally the partial html model.html

    Model: <select ng-model="selected_model" 
    ng-options="model.id_model as model.name for model in models" ></select>
    

    basically I wanted to point that piece "model.id_model as model.name for model in models" the "model.id_model" uses the id of the model for the value so that you can match with the "mainObj.id_model" which is also the "selected_model", this is just a plain value, also "as model.name" is the label for the repeater, finally "model in models" is just the regular cycle that we all know about.

    Hope this helps somebody, and if it does, please vote up :D

    0 讨论(0)
  • 2020-11-22 12:01

    The angular documentation for select* does not answer this question explicitly, but it is there. If you look at the script.js, you will see this:

    function MyCntrl($scope) {
      $scope.colors = [
        {name:'black', shade:'dark'},
        {name:'white', shade:'light'},
        {name:'red', shade:'dark'},
        {name:'blue', shade:'dark'},
        {name:'yellow', shade:'light'}
      ];
      $scope.color = $scope.colors[2]; // Default the color to red
    }
    

    This is the html:

    <select ng-model="color" ng-options="c.name for c in colors"></select>
    

    This seems to be a more obvious way of defaulting a selected value on an <select> with ng-options. Also it will work if you have different label/values.

    * This is from Angular 1.2.7

    0 讨论(0)
  • 2020-11-22 12:03

    So assuming that object is in your scope:

    <div ng-controller="MyCtrl">
      <select ng-model="prop.value" ng-options="v for v in prop.values">
      </select>
    </div>
    

     

    function MyCtrl($scope) {
      $scope.prop = {
        "type": "select", 
        "name": "Service",
        "value": "Service 3", 
        "values": [ "Service 1", "Service 2", "Service 3", "Service 4"] 
      };
    }
    

    Working Plunkr: http://plnkr.co/edit/wTRXZYEPrZJRizEltQ2g

    0 讨论(0)
  • 2020-11-22 12:04

    Just to add up, I did something like this.

     <select class="form-control" data-ng-model="itemSelect" ng-change="selectedTemplate(itemSelect)" autofocus>
            <option value="undefined" [selected]="itemSelect.Name == undefined" disabled="disabled">Select template...</option>
            <option ng-repeat="itemSelect in templateLists" value="{{itemSelect.ID}}">{{itemSelect.Name}}</option></select>
    
    0 讨论(0)
  • 2020-11-22 12:10

    If anyone is running into the default value occasionally being not populated on the page in Chrome, IE 10/11, Firefox -- try adding this attribute to your input/select field checking for the populated variable in the HTML, like so:

    <input data-ng-model="vm.x" data-ng-if="vm.x !== '' && vm.x !== undefined && vm.x !== null" />
    
    0 讨论(0)
  • 2020-11-22 12:11

    The ng-model attribute sets the selected option and also allows you to pipe a filter like orderBy:orderModel.value

    index.html

    <select ng-model="orderModel" ng-options="option.name for option in orderOptions"></select>
    

    controllers.js

    $scope.orderOptions = [
        {"name":"Newest","value":"age"},
        {"name":"Alphabetical","value":"name"}
    ];
    
    $scope.orderModel = $scope.orderOptions[0];
    
    0 讨论(0)
提交回复
热议问题