在检索数据之前运行的 Angular ng-repeat 过滤器 [英] Angular ng-repeat filter running before data retrieved

查看:22
本文介绍了在检索数据之前运行的 Angular ng-repeat 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约会列表,需要按工作人员筛选.我正在使用 Breeze 控制器从数据库中提取约会.某些任命涉及多个工作人员,因此我需要深入到任命的子实体中以查找每个任命的工作人员.我正在尝试根据在其他地方使用 ng-repeat 和过滤器选择的工作人员显示一个列表,如下所示:

I have a list of appointments that I need to filter by staff member. I am pulling the appointments from a database using a Breeze controller. Some appointments have multiple staff members involved, so I need to drill into a child entity of the appointment to find the staff members for each appointment. I am trying to show a list based on the staff member selected elsewhere using ng-repeat and a filter as follows:

<section data-ng-repeat="appt in apptCtrl.appointmentList | filter: apptCtrl.matchStaff(staff)" data-ng-model="apptCtrl.appointmentList" class="apptStaffList">
    <appt-event data-ng-model="appt"></appt-event>
</section>

我的约会控制器上的过滤功能如下:

The filter function on my appointment controller is as follows:

self.matchStaff = function (query) {
    var staffAppts = [];
    angular.forEach(self.appointmentList, function (a) {
        var staffAppt = a.some(function (s) {
            return s.StaffId === query;
        });
        if (staffAppt) {
            staffAppts.push(a);
        }
    });
    return staffAppts;
};

不幸的是,self.appointmentList 在控制器初始化时被填充,但列表直到视图已经加载后才返回.如果没有过滤器,则在填充列表后会立即显示约会.然而,当调用 matchStaff 时,self.appointmentList 只是一个空数组.

Unfortunately, the self.appointmentList is populated when the controller is initialized, but the list does not return until after the view has already loaded. Without the filter, the appointments appear as soon as the list is populated. When matchStaff is called, however, self.appointmentList is just an empty array.

有没有办法让过滤器在列表填充后运行?有没有其他方法来处理过滤器?在这种情况下,有没有办法用字符串调用过滤器?

Is there a way to cause the filter to run AFTER the list is populated? Is there some other way to handle filters? Is there a way to call the filter with a string in this case?

预先感谢您的帮助!

推荐答案

您可以在 resolve 方法.

//config
    $routeProvider
        .when('/',
        {
          templateUrl: "app.html",
          controller: "AppCtrl"
          resolve: {
            appointments: function (myAppoimentsService) {
              return myAppoimentsService.fetch();
            }
          }
        }

...

// controller
app.controller('AppoimentCtrl', ['appointments', function (appointments) {

  this.appoimentList = appointments;
}]);

这篇关于在检索数据之前运行的 Angular ng-repeat 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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