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

查看:57
本文介绍了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?

提前感谢您的帮助!

推荐答案

您可以将约会列表加载到解决方法(如果您在应用程序上使用路由的话).

You can load the appointmentList in a resolve method if you are using routes on your application.

//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天全站免登陆