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

1 Answer 1

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

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

3 Comments

This is hard coded. Looking for dynamic solution, I need to set using <select>. using ng-init or ng-selected.
even if you use ng-init its hardcoding dude
That is the requirement, default = Y. That should be the condition. Can't set json value directly, that will be dynamic.

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.