如何在选择框中默认选项 - Angular.js [英] How to have a default option in select box - Angular.js

查看:106
本文介绍了如何在选择框中默认选项 - Angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查谷歌和无法找到这样的东西。

我有这样的code。

 <选择NG模型=somethingHere
        NG-选项=option.value作为option.name在选项选项
>< /选择>

通过这样的一些数据

 选项= {[
   名称:'一些很酷的东西,
   值:什么爽的价值
},{
   名称:'别的东西',
   值:'东西,别的价值
}];

和输出是这样的。

 <选择NG模型=somethingHere
        NG-选项=option.value作为option.name在选项选项
        类=NG-原始NG-有效>    <?期权价值=选择=选择>< /选项>
    <期权价值=0>一些很酷的东西与LT; /选项>
    <期权价值=1>别的东西< /选项>
< /选择>

这怎么可能设置数据为默认值的第一个选项,这样你会得到这样的结果。

 <选择NG模型=somethingHere......>
    <期权价值=选择=选择和0GT;一些很酷的东西与LT; /选项>
    <期权价值=1>别的东西< /选项>
< /选择>


解决方案

您可以简单地使用 NG-INIT 像这样

 <选择NG-的init =somethingHere =选项[0]
        NG-模式=somethingHere
        NG-选项=option.name在选项选项>
< /选择>

I have searched Google and can't find anything on this.

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>

解决方案

You can simply use ng-init like this

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

这篇关于如何在选择框中默认选项 - Angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆