选择角+:选定值

编程入门 行业动态 更新时间:2024-10-27 16:35:04
本文介绍了选择角+:选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于选择的部件和AngularJS的组合设定NG-模式问题(见视频:的 www.youtube/watch?v=8ozyXwLzFYs )

I have a question about setting ng-model in combination of chosen widget and AngularJS (see this video : www.youtube/watch?v=8ozyXwLzFYs)

我想这样做的是收件人设置为某个值,这将是preselected。这里是HTML和JS这个例子(从视频)结果

What I would like to do is to set recipients to some values which would be preselected. Here is HTML and JS for this example (from the video)

<h1>Chosen</h1> <select data-placeholder="Choose a Recipient" multiple class="span4 chzn-select" chosen ng-model="recipients" ng-options="recipient.name for recipient in recipientsList"> </select> <p ng-repeat="recipient in recipients"><strong>{{recipient.name}}</strong></p>

和JS的控制器结果

$scope.recipientsList = []; $scope.recipients = []; $scope.fetchRecipients = function() { $http.get($scope.url).then(function(result){ $scope.recipientsList = [ {"id":0, "name":"Recipient 0"}, {"id":1, "name":"Recipient 1"}, {"id":2, "name":"Recipient 2"}, {"id":3, "name":"Recipient 3"}, {"id":4, "name":"Recipient 4"}, {"id":5, "name":"Recipient 5"}, {"id":6, "name":"Recipient 6"}, {"id":7, "name":"Recipient 7"}, {"id":8, "name":"Recipient 8"}, {"id":9, "name":"Recipient 9"}, {"id":10, "name":"Recipient 10"} ]; $scope.recipients = [{"id":0, "name":"Recipient 0"}, {"id":1, "name":"Recipient 1"}]; }); } $scope.fetchRecipients();

我尝试了一些组合,但在菜单值不是preselected,尽管它们被存储在接受者,因为他们是下面的菜单显示。你可以在这里看到这样的例子: jsfiddle/YKZSw/8/

感谢您ansers。

马捷

推荐答案

AngularJS ngOptions属性选择从数据源提供的数组中的元素。在你的情况下,recipientsList数组包含一定数目的对象。然而,以preSELECT你希望的元件不因反对不平等现象,即使它们是相似的名单present。含义recipientsList:{ID:0,名:收件人0}不等于收件人:{ID:0,名:收件人0}(你可以试试在JS相同安慰)。角度没有找到在列表元素,因此不preSELECT

AngularJS ngOptions attribute selects an element from the array provided as data source. In your case, the recipientsList array contains certain number of objects. Yet, the elements you wish to preselect are not present in the list due to object inequality even though they are similar. Meaning recipientsList: {"id":0, "name":"Recipient 0"} is not equal to recipients:{"id":0, "name":"Recipient 0"}(You can try the same in the JS console). Angular does not find the element in the list and hence does not preselect.

此修改小提琴作品: jsfiddle/mananbharara/YKZSw/9/在这种情况下,唯一的区别是在你的收件人定义:

This modified fiddle works: jsfiddle/mananbharara/YKZSw/9/ The only difference in this case is in your recipients definition:

$ scope.recipients = [$ scope.recipientsList [0],$ scope.recipientsList [1];

在这种情况下,你有一个列表,选择的值只能来自该列表中。

In this case you have one list and selected values can come from that list only.

这种方法,你会在使用的情况下,你必须保持两个列表的替代方法是保持选择值作为基本类型。在这种情况下,原始的平等总会举行。

An alternative to this approach which you would use in case you have to maintain two lists is to keep the select value as a primitive type. In which case primitive equality would always hold.

更多推荐

选择角+:选定值

本文发布于:2023-11-06 08:04:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!