微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – ng-model和ng-options不匹配?

我在我的资源对象中有一个方法,它来自:

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.

原文地址:https://www.jb51.cc/angularjs/141338.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐