angularjs – ng-model和ng-options不匹配?
发布时间:2020-12-17 08:33:10 所属栏目:安全 来源:网络整理
导读:我在我的资源对象中有一个方法,它来自: resources.type otherstuff: 'more strings'type:'specifictype'morestuff: 'morestuff' 用户可以通过下拉列表/通过另一个调用来更改此类型,该调用获取所有可能类型的列表,这些类型看起来像resourceList.types,其中包
我在我的资源对象中有一个方法,它来自:
resources.type otherstuff: 'more strings' type:'specifictype' morestuff: 'morestuff' 用户可以通过下拉列表/通过另一个调用来更改此类型,该调用获取所有可能类型的列表,这些类型看起来像resourceList.types,其中包含像json这样的对象列表 types: [ {name:'resourcetype1'},{name:'resourcetype2'},etc... ], 我的HTML看起来像: <select ng-model="resources.type" ng-options="name.name for name in resourceList.types"> </select> 选择/下拉框填充了我的resourceList.type内容,但是当页面加载时,ng-model没有设置为已选择的资源类型.实际上,当您单击时,它会在下拉列表的顶部选择一个空白条目.对此有棱界吗?如何让ng-model按照我想要的方式运行? 我尝试用不同的方法来解决重复问题,但我觉得这更像是角度连接模型的方式.它是否只是将字符串与ng-options列表匹配? 这是plnkr,你可以看到它不是默认为type1 http://plnkr.co/edit/NyWACtFQuyndR6CG8lpN?p=info
在Angular中,模型是真理的唯一来源.
这意味着如果您想要选择一个值(并绑定到您的ngModel),您需要将其分配给模型: <select ng-model="resources.type" ng-options="type.name as type.name for type in resourceList.types"> </select> $scope.resources = {...}; $scope.resourceList = { ... types: [ {name: 'resourcetype1'},{name: 'resourcetype2'},... ] }; // Set the model to the desired value $scope.resources.type = $scope.resourceList.types[0].name; 另见这short demo. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Scala案例类在receive方法中不匹配(在akka actor
- 使用AngularFire2访问firebase.storage()(Angula
- 使用变量间接扩展的Bash数组扩展
- scala – 为什么我在一个案例中得到“缺少扩展函
- Angular 4,将http响应observable转换为object ob
- vim 常用快捷键
- webService——学习(3):使用JDK开发webServic
- 360 DoctorStarange:集预测、处理、关联和资源优
- 从shell和sbt在android sdk中运行aapt的程序
- Symbian学习笔记(17) - 初探WebServices API的使
热点阅读