Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Data binding default value of html select

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?

like image 669
ncohen Avatar asked Oct 31 '22 23:10

ncohen


1 Answers

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.

like image 118
Brandon Horst Avatar answered Nov 08 '22 05:11

Brandon Horst