Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

javascript - 如何在Angular.js选择框中使用默认选项(How to have a default option in Angular.js select box)

I have searched Google and can't find anything on this.(我已经搜索过Google,但找不到任何东西。)

I have this code.(我有这个代码。) <select ng-model="somethingHere" ng-options="option.value as option.name for option in options" ></select> With some data like this(有了这样的数据) options = [{ name: 'Something Cool', value: 'something-cool-value' }, { name: 'Something Else', value: 'something-else-value' }]; And the output is something like this.(输出是这样的。) <select ng-model="somethingHere" ng-options="option.value as option.name for option in options" class="ng-pristine ng-valid"> <option value="?" selected="selected"></option> <option value="0">Something Cool</option> <option value="1">Something Else</option> </select> How is it possible to set the first option in the data as the default value so you would get a result like this.(如何将数据中的第一个选项设置为默认值,这样您将得到这样的结果。) <select ng-model="somethingHere" ....> <option value="0" selected="selected">Something Cool</option> <option value="1">Something Else</option> </select>   ask by iConnor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply use ng-init like this(您可以像这样简单地使用ng-init)

<select ng-init="somethingHere = options[0]" ng-model="somethingHere" ng-options="option.name for option in options"> </select>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...