0

when am using this code.selected value shown as empty.i want select 1st value as one.

  <select class="form-control" name="cityID" id="Select1" ng-change="RoomSelect()" ng-model="sRoom">
                                            <option value="1" selected="selected">One</option>
                                            <option value="2">Two</option>
                                            <option value="3">Three</option>
                                            <option value="4">Four</option>
                                        </select>

When am using ng-model at the time default value selected as empty.otherwise its ok.

2
  • try init sRoom model in controller Commented Feb 28, 2017 at 5:29
  • try setting sRoom=1 in your controller. Commented Feb 28, 2017 at 5:30

3 Answers 3

2

Try init sRoom model in controller

 $scope.sRoom = 1;

also its better use ng-options instead of repeat option in select tag.

Demo

var app = angular.module('anApp', []);
app.controller('aCtrl', function($scope) {
  $scope.sRoom = 1;
  
  $scope.chooses = [{"key":"one","value":1},{"key":"Two","value":2},{"key":"Three","value":3}];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="anApp" ng-controller="aCtrl">
    <select ng-change="RoomSelect()" ng-model="sRoom" ng-options="k.value as k.key for  k in chooses">
     </select>
</div>

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

Comments

0

Try this

<select class="form-control" ng-init="sRoom=1" name="cityID" id="Select1" ng-change="RoomSelect()" ng-model="sRoom">
                                            <option value="1" selected="selected">One</option>
                                            <option value="2">Two</option>
                                            <option value="3">Three</option>
                                            <option value="4">Four</option>
                                        </select>

Comments

0

You can also use ng-init

<select class="form-control" ng-init="sRoom=1"  name="cityID" id="Select1" ng-change="RoomSelect()" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.