5

I would like to 'data bind' the default value of a html/jade select. Here is my select:

select#title.form-control(ng-model='newClient.title')
    option(ng-repeat='title in titles', ng-selected='$first') {{title}}

The problem here is that if I don't touch the select, my newClient.title will be set as undefined and not the first title value. How can I do that without setting a default value in my controller?

4
  • How about ng-selected="newClient.titles[0]" Commented May 23, 2014 at 13:08
  • ng-selected is a boolean, newClient.title is a string... I don't understand what you're trying to do? Commented May 23, 2014 at 13:12
  • Ahh, nvm, just saw you had the repeat on option elements - why are you not using ng-options? Commented May 23, 2014 at 13:16
  • B ecause I'm new to angularJS and I don't have the right reflex :o) Let me try that... Commented May 23, 2014 at 13:20

2 Answers 2

2

You want to use ng-options to define the options, and then simply specify the ng-model for the first element. You don't have to worry about an ng-init.

<select ng-options="title for title in titles" ng-model='titles[0]'></select>

For reference, there's some pretty good documentation here.

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

Comments

0

You can use a ng-init directive.

ng-init="newClient.title = titles[0]"

Fiddle Here

in jade format:

select#title.form-control(ng-model='newClient.title',ng-init="newClient.title = titles[0]")
    option(ng-repeat='title in titles', ng-value="title") {{title}}

Comments

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.