2
<select ng-model="machineSelected"
        ng-options="machine._id as machine.name for machine in machineList"
        ng-change="onChangeMachine(machineSelected)"
        ng-disabled="!mineSelected">
    <!--<option value=""></option>-->
    <option value="">Select</option>
</select>

When I add

$scope.machineSelected = "";

dynamically wherever in the controller, option in drop-down should set as "Select", but it is not updating.

4
  • I don't understand what is your problem Commented Jul 24, 2018 at 12:57
  • what's the default value? Would $scope.machineSelected = machineList[0]._id; work? Commented Jul 24, 2018 at 13:01
  • When i add $scope.machineSelected = " " , dynamically whereever in the controller, option in dropdown should set as Select , but it is not updating. Commented Jul 24, 2018 at 13:01
  • @AlekseySolovey --- 'Select' is not available in machinelist , so machineList[0]._id contains random id number but not 'Select' ... How to handle in these situation Commented Jul 24, 2018 at 13:06

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The ng-options directive was re-factored with AngularJS V1.6. Before then the default could be selected with an empty string. Now the default is selected by assigning null to the model.
@georgeawg Tried it (plnkr) with different old versions. You're right. It's been changed in 1.6.5.
0

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

2 Comments

Pls accept and upvote it if you found the answer helpful
I don't have enough reputation points to upvote , although i upvoted
0

Use null for the unselected value:

̶$̶s̶c̶o̶p̶e̶.̶m̶a̶c̶h̶i̶n̶e̶S̶e̶l̶e̶c̶t̶e̶d̶ ̶=̶ ̶"̶"̶;̶
$scope.machineSelected = null;

The ng-options directive was re-factored with AngularJS V1.6. Before then the default could be selected with an empty string. Now the default is selected by assigning null to the model.

For more infomation, see

The DEMO

angular.module("app",[])
.controller("ctrl",function($scope){
  $scope.machineList = [
    { _id:1, name: "Megatron" },
    { _id:2, name: "Starscream" },
    { _id:3, name: "Shockwave" },
  ];
  $scope.machineSelected = null;
})
<script src="//unpkg.com/angular/angular.js"></script> 
<body ng-app="app" ng-controller="ctrl">
    <select ng-model="machineSelected"
        ng-options="machine._id as machine.name for machine in machineList"
        ng-change="onChangeMachine(machineSelected)">
       <option value="">Select</option>
    </select>
    <br>
    machineSelected={{machineSelected}}
</body>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.