0

Issues fiddle here dropdown issue with default selection Below the JSON used to populate the drop down.

        {
            "key": "all",
            "value": "First",
            "default":"N"
        },       
        {
            "key": "only_my_events",
            "value": "Default",
            "default":"Y"
        },
        {
            "key": "only_assigned",
            "value": "Second",
            "default":"N"
        },
        {
            "key": "only_owner",
            "value": "Third",
            "default":"N"
        },
        {
            "key": "none",
            "value": "Fourth",
            "default":"N"
        }

Drop down code to display the item

 <select ng-model="mail_notification" ng-options="c.key as c.value for c in mail_notifications" ng-init = "c.default = 'Y'"></select>

Required

The default item selected should be the item with default value is Y (default = 'Y').

Issues.

First item in the drop down is empty. there is no default selected item.

Already tried.

Seen solution like setting a default value like " Choose an Item", which is not im looking for. AngularJS. When select have ng-model attribute, ng-selected not working

georgeawg
  • 48,608
  • 13
  • 72
  • 95
user630209
  • 1,123
  • 4
  • 42
  • 93

1 Answers1

1

It's simple, you just need to set the ng-model as mail_notification

$scope.setDefault = function(){
  $scope.mail_notification = $scope.mail_notifications.find(t=>t.default === 'Y').key;
  console.log($scope.mail_notification);
}

and in HTML

  <select ng-model="mail_notification" ng-options="c.key as c.value for c in mail_notifications" ng-init = "setDefault()"></select>

JSFIDDLE DEMO

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396