0

I have the following select:

<select class="form-control" id="selSigningAuthorityDisplayName"
        ng-model="selectedUser.SigningAuthority"
        ng-options="signingAuthority as signingAuthority.SigningAuthorityDisplayName for signingAuthority in signingAuthorityList track by signingAuthority.SigningAuthoritySystemName">
</select>

This works perfectly when I'm editing an existing user profile and the selectedUser.SigningAuthority object is populated. The drop down defaults to the value of SigningAuthority as expected. But when I try to add a new user profile and selectedUser.SigingingAuthority is null I want the drop down list to default to a specific value. How do I tell angular to select a specific default value when selectedUser.SigningAuthority is null?

Kaelan Fouwels
  • 1,155
  • 1
  • 13
  • 28
Legion
  • 3,922
  • 8
  • 51
  • 95
  • 1
    Set `SigningAuthority` to the default value when you first create `selectedUser`. Your code should be setting up your model for your UI to react to, not the other way around. – Rhumborl Dec 13 '15 at 17:51
  • For some reason this doesn't work. There's no error thrown, but setting selectedUser.SigningAuthority.SigningAuthoritySystemName = "SME", which is the default value I want isn't reflected in the drop down. The drop down just defaults to a blank selection, forcing the user to manually select an option. – Legion Dec 13 '15 at 18:16
  • Read the answer you got. It explains how to do it. The selectedUser.SigningAuthority object must be one of the objects in the array of selectable options. – JB Nizet Dec 13 '15 at 19:02

1 Answers1

1

Add default option to select tag, it's identical as ng-model=null

<select class="form-control" id="selSigningAuthorityDisplayName" ng-model="selectedUser.SigningAuthority" ng-options="signingAuthority as signingAuthority.SigningAuthorityDisplayName for signingAuthority in signingAuthorityList track by signingAuthority.SigningAuthoritySystemName">
     <option value="">Not selected</option>
</select>

Or you can assign value to ng-model inside controller;

$scope.selectedUser = {};
$scope.selectedUser.SigningAuthority = $scope.selectedUser.SigningAuthority || $scope.signingAuthorityList[0]; 
//or whatever index; But note, your specific value must be inside this list
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69