angularJs中的Limito和Filter

angularJs中的Limito和Filter,angularjs,Angularjs,我想使用filter从ngbind中的数组中获得一个结果。我想做如下事情 <h1 ng-bind="item.Description as item in Items | filter: {id: someID}"></h1> 我想将h1设置为item。DescriptionfromItems其中someID来自下拉列表。这意味着,当我从下拉列表中选择时,h1通过SomeID过滤项来设置。这就像是h1=Items.FirstOrDefault(x=>x.id==so

我想使用
filter
ngbind
中的数组中获得一个结果。我想做如下事情

<h1 ng-bind="item.Description as item in Items | filter: {id: someID}"></h1>


我想将
h1
设置为
item。Description
from
Items
其中
someID
来自下拉列表。这意味着,当我从下拉列表中选择时,
h1
通过
SomeID
过滤
项来设置。这就像是
h1=Items.FirstOrDefault(x=>x.id==someID)。Description

如果您有一个下拉列表,那么您知道选择是什么(通过ng模型)。您拥有的代码正在使用ng bind,但它是ng repeat的文本。最好在控制器上设置一个方法:

getItemDescription() {
  const selection = this.form.dropdown.selection; // Holds the dropdown 'selection' object (from the ng-model on your select dropdown);
  return Items.find((item) => item === selection).description;
}
然后您的HTML变成:

<h1 ng-bind="vm.getItemDescription()"></h1>

这里的问题是
ng bind
无法在items
语法中重新编码
项。但正如您在标题中所提到的,您可以利用
limito
ng repeat
来实现大致相同的结果。像这样:

<h1 ng-repeat="item in items | filter: {id: someId} | limitTo: 1">
    {{item.Description}}
</h1>

{{item.Description}
(为了简单起见,将数字输入用于
someId