Ember select未显示所选项目

Ember select is not showing the selected item

本文关键字:选项 项目 显示 select Ember      更新时间:2023-09-26

我知道有一个小问题正盯着我的脸,但我无法解决。

考虑

{{view Ember.Select                         
     content=baseList          
     optionLabelPath="content.desc"
     optionValuePath="content.id"
     selectionBinding="selectedItem" 
 }}
baseList = [{"id":"item1","desc":"item number is 1"},{"id":"item2","desc":"item number is 2"}] 

以下不起作用

selectedItem = {"id":"item1","desc":"item number is 1"};

选择下拉菜单不显示任何选择的项目

以下作品

selectedItem = baseList.filterBy('id','item1')[0];

现在,选择下拉菜单显示所选项目。

问题出在哪里?我甚至检查了属性(id和desc)的顺序是否正确。是因为除非采用某种算法或使用JSON.stringify,否则无法直接比较两个对象吗?

问题是,当您指定时

selectedItem = {"id":"item1","desc":"item number is 1"};

该散列与baseList中的散列是不同的对象,尽管它在词汇上是相同的。所以Ember在baseList中找不到它(它正在进行===比较,而不是深度比较)。另一方面,当执行filterBy时,它从baseList中返回实际对象,然后Ember.Select可以在baseList中找到该对象。

您可能需要尝试使用valueBinding;则可以指定CCD_ 10。

顺便说一句,属性的顺序在这里或JS中的其他任何地方都没有区别。