加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs中下拉框select option默认值

发布时间:2020-12-17 09:44:13 所属栏目:安全 来源:网络整理
导读:http://www.111cn.net/wy/js-ajax/101967.htm 今天又被一个angularjs的一个问题给难住了,有花了比较多的时间去解决 option ng-repeat多空白项 selectclass="form-control" ng-model="posts.website" option value="0"请选择网站/option option ng-repeat="r

http://www.111cn.net/wy/js-ajax/101967.htm


今天又被一个angularjs的一个问题给难住了,有花了比较多的时间去解决

option ng-repeat多空白项


<selectclass="form-control" ng-model="posts.website">
<option value="0">请选择网站</option>
<option ng-repeat="row in list.websites" value="{{row.websites_id}}" >{{row.websites_name}}</option>
</select>
请看上面的代码,ng-repeat在循环的时候发现了一个空白的选项,chrome解析出来代码是

chrome解析错误代码


<option value="?undefined:undefined ?"></option>

然后查了list.websites对象的值貌似没有问题

然后google一下,这里又要吐槽一下百度查不到结果
发现要初始化posts.website的值,于是用ng-init初始一下值,把代码修改成

<select class="form-control" ng-model="posts.website" ng-init="posts.website='0'">
<option value="0">请选择网站</option>
<option ng-repeat="row in list.websites" value="{{row.websites_id}}" >{{row.websites_name}}</option>
</select>

正常了,好坑啊

这文章里讲了怎么解决添加页面的option多出来的问题

在修改页面里我也是安装这个方法去处理,但是不行,给ng-model附上值就是显示不了默认值,最后在自己的摸索下解决了这个问题

正确方面

<select ng-model="posts.website" ng-options="c.websites_id as c.websites_name for c in posts.websites" class="form-control" ng-selected="c.websites_id==posts.website">
<option value="">请选择网站</option>
</select>

游览器解析的代码为

<select ng-model="posts.website" ng-options="c.websites_id as c.websites_name for c in posts.websites" class="form-control ng-pristine ng-valid ng-touched" ng-selected="c.websites_id==posts.website"> <option value="" class="" selected="selected">请选择网站</option> <option label="fdsaffd" value="number:4">fdsaffd</option> <option label="fdsaffdsa" value="number:3">fdsaffdsa</option> <option label="fdsaf" value="number:2">fdsaf</option> <option label="小松博客" value="number:1" selected="selected">小松博客</option> </select>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读