简体   繁体   English

如何在AngularJS选择中显示当前选定选项的缩短文本?

[英]How can I display a shortened text for the currently selected option in an AngularJS select?

I have a select. 我有一个选择。 Some of the options in the select are very, very long, and the space for the select to sit in is quite short. 选择项中的某些选项非常非常长,并且可供选择项放置的空间非常短。 For reasons of non-native select design hacks, I need the currently-selected option text to be shorter than the space the select element actually occupies. 由于非本地选择设计的原因,我需要当前选择的选项文本要短于select元素实际占用的空间。

I want the full text to be displayed when the element is active, eg when all the options are displayed. 我希望在元素处于活动状态时(例如,在显示所有选项时)显示全文。 Once an object is selected and the element is not active, I want a shorter text to be displayed. 一旦选择了对象并且该元素未处于活动状态,我希望显示较短的文本。

I can do this by hiding the select and putting the shortened text into an element positioned absolutely over the select, but this brings up a world of cross-browser pain- hiding the select while allowing it to activate when clicked has EXTREMELY variable results across different platforms. 我可以通过隐藏选择内容并将缩短的文本放入绝对位于选择内容上方的元素中来完成此操作,但这带来了跨浏览器的痛苦世界:隐藏选择内容,同时允许其在单击时被激活,从而在不同的地方具有EXTREMELY变量结果平台。

Here's a Plunkr showing an example of this technique. 这是一个显示此技术示例的Plunkr

<div class="has-select">
  <select ng-model="activity" ng-options="c.name for c in options" ng-init="activity = options[0]">
  </select>
  <span class="short">
    {{ activity.name | limitTo: 30 }}
  </span>
</div>

Does anyone know a way to do this that would use only the standard select mark-up and not rely on additional elements? 有谁知道仅使用标准选择标记而不依赖其他元素的方法吗?

There are a few problems to achieving this: 要实现这一点,存在一些问题:

  • You can't use a smart "substring" function in Angular's ng-option since this will cause a rebind. 您不能在Angular的ng-option中使用智能的“子字符串”功能,因为这会导致重新绑定。

  • Even if you have a logic triggered substring this won't affect the "dropdown" when it is active, seems like the browser (at least Chrome) is binding the data in the dropdown before ng-click happen, badly written example here . 即使您有一个逻辑触发的子字符串,当它处于活动状态时,它也不会影响“下拉列表”,似乎浏览器(至少是Chrome)在ng-click发生之前绑定了下拉列表中的数据, 此处的示例写得不好

My recommendation would be to go with a "custom select" implementation similar to FlatUI (but Angular style of course!). 我的建议是使用类似于FlatUI的“自定义选择”实现(当然是Angular风格!)。 This will probably be the least painful way to achieve this. 这可能是实现这一目标的最痛苦的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM