0

I have a dropdown, I'm binding it with owners, but its giving extra blank option. I want to remove the blank option from it. I'm able to do but when I add these changes my required validator does show error.

<md-input-container>
        <select name="owner" ng-model="form.owner" ng-options="owner.UserName for owner in owners"
                class="input-div" style="height:30px" ng-change="ownerTypeChange()" required ng-disabled="update && view">
          <option style="display:none" value=undefined>Select an owner</option>
        </select>
        <div ng-messages="DataAccessRoleForm.owner.$error">
          <div ng-message="required">required</div>
        </div>
      </md-input-container>

If I remove the below line, then required validator works but dropdown has the blank option.

<option style="display:none" value=undefined>Select an owner</option>
4
  • 1
    on your controller make your model equal to the first element of your options array form.owner = owners[0] to pre select your first option Commented Jul 6, 2017 at 13:48
  • but I don't want to select anything by default. Commented Jul 6, 2017 at 14:00
  • then you will have an empty option ¯\_(ツ)_/¯ Commented Jul 6, 2017 at 14:01
  • @FabioAntunes cute smiley :P Commented Jul 6, 2017 at 14:03

1 Answer 1

1

Check if your select has been touched before displaying the required message.

<div ng-if="form.owner.$touched" ng-message="required">required</div>

You can also set the default option to be selected but not selectable once they open the drop down.

<option value="" disabled selected hidden>Select an owner</option>
Sign up to request clarification or add additional context in comments.

1 Comment

Getting this error. Controller 'ngMessages', required by directive 'ngMessage', can't be found!

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.