javascript - select ng-options 通过id拿到name 请各位大神帮忙解决
高洛峰
高洛峰 2017-04-11 12:08:42
[JavaScript讨论组]
  

controller里面这样写的

$scope.interestes=[{
        id:'0',
        name:'通用兴趣'
    },{
        id:'1',
        name:'通用兴趣1'
    },{
        id:'2',
        name:'通用兴趣2'
    },{
        id:'3',
        name:'通用兴趣3'
    },{
        id:'4',
        name:'通用兴趣4'
    },{
        id:'5',
        name:'通用兴趣5'
    }]
    
    $scope.interest=$scope.interestes[0].id;
    我想通过id把name拿到
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
迷茫

直接写

ng-options="interest as interest.name for interest in interestes"

然后用 $scope.interest.name 去获取name不就是了

怪我咯

当数据源为数组时,ngOptions有以下传参方式。

label for value in array
select as label for value in array
label group by group for value in array
select as label group by group for value in array track by trackexpr

其中select表示ngModel最后绑定的值,label则是表示选择在选择时显示的值。
话说根据id获取name不是直接可以在controller里面就能完成么。为什么要写个select。

黄舟

jQuery.grep
楼主想要通过ID在interestes获取name属性,除了迭代没有其它办法,上面是jQuery提供的工具函数,非常好用。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号