-1

I have a drop down using angularjs but at initial loading it showing 'nothing selected' ,I want select First value default.

<select id="cbo-db-selection" ng-init="selectedlibrary=selectedlibrary[0].value" ng-model="selectedlibrary" required="required" ng-options="opt.DatabaseName as opt.DatabaseName for opt in DBLibraries">
  <option style="display:none" value="">{{selectedlibrary}}</option>
</select>
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Night Watcher
  • 41
  • 1
  • 1
  • 4
  • I guess this post can help you : [HERE](http://stackoverflow.com/questions/18194255/how-to-have-a-default-option-in-select-box-angular-js) You have to use a ng-init – Adrien Blaizot Dec 31 '15 at 09:03

1 Answers1

1

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
    $scope.users = [
        {id: 1, name: 'Me'},
        {id: 2, name: 'You'}
    ];
});

app.directive('select', function () {
    return {
        restrict: 'E',
        require: '?ngModel',
        priority: 10,
        link: function postLink(scope, elem, attrs, ctrl) {
            if (!ctrl) { return; }
            var originalRender = ctrl.$render.bind(ctrl);
            ctrl.$render = function () {
                originalRender();
                if (elem.val() === '?') {
                    ctrl.$setViewValue(undefined);
                }
            };
        }
    };
});
.error {
    color: Red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" ng-form="form1">

  <select ng-model="userId">
    <option ng-repeat="user in users"
            value="{{user.id}}">
      {{user.name}}
    </option>
</select>
  
    
    <div class="error" ng-show="form1.select1.$invalid">Required field !</div>
    <div class="error" ng-show="form1.$invalid">Invalid form !</div>
    <br />
    <button ng-click="userId=1;">Set valid value</button>
    <button ng-click="userId=3;">Set invalid value</button>
    <hr />
    <pre>userId: {{userId}}</pre>
</div>
Suresh B
  • 1,658
  • 1
  • 17
  • 35