在 ng-repeat 中选择和访问项目 [英] Selecting and accessing items in ng-repeat

查看:19
本文介绍了在 ng-repeat 中选择和访问项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这样的视图中显示服务中的一组对象

I have an array of objects in a service that I want to display in a view like this

<li ng-repeat="item in filteredItems = (resource.items | filter:multipleFilters...">
    <span class="title">{{item.escaped_name}}</span>
</li>

我希望这些对象是可选择的,这很容易.然后我需要获取所选项目的数量,并能够遍历所有项目以处理/更改数据.

I want these objects to be selectable which is the easy part. Then I'd need to get the count of the selected items and be able to iterate over all of them to process/change data.

保存和访问所选项目的最佳方法是什么?

What is the best method to save and access the selected items?

请注意,所选项目可能会发生变化,例如,所选项目可能会因过滤器更改而丢失.此外,我不想直接在数组中的对象上设置选定的属性 - 对象数组在一个服务中并在许多列表中的整个应用程序中使用,我不想清理"选定的属性对于每个视图.

Note that the selected items could change, for example a selected item could fall away due to changed filters. Also, I don't want to set a selected property on the objects in the array directly - the array of objects is in a service and used throughout the app in many lists and I don't want to "clean up" the selected property for each view.

推荐答案

我通常包含一个 ng-click 标签:

I usually include a ng-click tag:

<li ng-repeat="item in ..." ng-click="select_item(item)">

传递给 select_item()item 将是用户选择的那个.

The item passed to select_item() will be the one the user selected.

这篇关于在 ng-repeat 中选择和访问项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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