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

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.

(编辑:李大同)

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

    推荐文章
      热点阅读