angularjs选择与过滤离开一线空,但不会在应用前期过滤器 [英] angularjs select with filter leaves first line empty but not when filter is applied upfront

查看:263
本文介绍了angularjs选择与过滤离开一线空,但不会在应用前期过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是有点失落,为什么我的NG选项再次返回一个空行带有过滤器。

I am a bit lost as to why my ng-options is once again returning an empty line with a filter.

请看看这个 plunker

我们的想法是在下拉列表中显示的是基于树形结构的对象上的组织结构图。

The idea is to display in a dropdown list an org chart which is based on a tree-structured object.

要做到这一点,我已经建立了平坦树成一个一维对象的服务。我用这个服务被称为在我的NG选项过滤器内。

To do this I have built a service that flattens the tree into a 1 dimensional object. I use this service inside a filter that is called in my ng-option.

<select ng-model="treeValue1" ng-init="treeValue1 = fields[0]" class='form-control' 
   ng-options="element as element.display for element in fields | flattenTree">
  </select> 

这将显示一个空行,当我选择一个值,它只是不选择它,然后又回到了空行。这是更加混乱。

this displays an empty line and when I select a value, it just doesn't select it and goes back to the empty line. That's even more confusing.

但是当我使用的扁平化功能在前期控制器,它工作得很好。

But when I use the flatten function upfront in the controller, it works nicely.

<select ng-model="treeValue2" ng-init="treeValue2 = flatFields[0]" class='form-control' 
   ng-options="element as element.display for element in flatFields">
  </select> 

我不知道为什么第一个不能正常工作。

I have no idea why the first one isn't working.

您的帮助将大大AP preciated,如果你能提供此行为的解释,这将是真棒。

Your help would be greatly appreciated and if you can provide an explanation for this behavior it would be awesome.

谢谢!

推荐答案

过滤器会创建一个新的数组所以基本上你的模型和你的选择是2个不同的对象,因此选择不承认模型值作为其选择,多数民众赞成的一部分为什么你没有在第一个选择。关于第二个你是选择从已过滤的数据集这样的模式不属于选项阵列模式

a filter creates a new array so basically your model and your options are 2 different objects hence the select doesn't recognizes the models value as part of its selection thats why you have nothing selected in the first one. on the second one you are selection the model from the already filtered data set so the model does belong to the options array

这篇关于angularjs选择与过滤离开一线空,但不会在应用前期过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆