Angular ng-repeat filter running before data retrieved

浪子不回头ぞ 提交于 2019-12-11 03:55:01

问题


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;
};

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?

Thanks in advance for your help!


回答1:


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;
}]);


来源:https://stackoverflow.com/questions/29809594/angular-ng-repeat-filter-running-before-data-retrieved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!