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
fromItems
其中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
)