how to bind default value to select using ng-model using AngularJS V1.6

前端 未结 3 659
[愿得一人]
[愿得一人] 2021-01-21 16:58

                        
    
提交评论

  • 2021-01-21 17:44

    Use null or undefined, not an empty string, as the "not selected" value.

    That's what the docs say:

    Optionally, a single hard-coded <option> element, with the value set to an empty string, can be nested into the <select> element. This element will then represent the null or "not selected" option. See example below for demonstration.

    It's not really clear that an empty string can't be used, but at least they mention null.

    0 讨论(0)
  • 2021-01-21 17:46

    Set default option in controller and dynamically change based on selected item

     <select ng-model="selectedItem"  ng-change="getSelectedText()">
        <option >All Items</option>
        <option >Active Subscriptions Only</option>
        <option >Expired Only</option>
     </select>
    

    Controller

    app.controller('SelectedTextController', function ($scope) {
        $scope.selectedItem = "All Items";
        $scope.getSelectedText = function () {
            var selectedItem=this.selectedItem;
        };       
    });
    
    0 讨论(0)
  • 提交回复
    热议问题