向工厂和控制器注入相同的角度服务

时间:2017-02-02 16:24:25

标签: angularjs

目前,为了加载至少100个客户预订记录,我们使用Angular工厂作为InitializationFactory,以便在页面加载之前掌握数据并且此工厂在路径中注册。该工厂使用 customerService 与相关API进行通信。整个请求需要大约5-7秒才能完成。我们需要在获取的数据之上应用过滤器。

考虑到页面性能,我们现在只想获取应用了动态过滤器的10条记录。将基于click事件检索下一组记录。现在,为了做到这一点,我还需要将 customerService 注入到CustomerController中。 因此,通过设计,将customerService注入工厂然后进入控制器进行进一步的数据检索是一种很好的方法吗?

代码段:

  1. Route.js

     .state("app.customer.dashboard",
        {
            parent: "parent",
            controller: "CustomerBookingController",
    
            resolve: {
                customerBookingListModel: [
                        "CustomerInitializationFactory",
                        function (customerInitializationFactory) {
                            return customerInitializationFactory.initializeCustomerBookingResult();
                        }
                ]
            }
    
  2. CustomerInitializationFactory.js

    function CustomerInitializationFactory(customerService, $translate, bookingConstants) {
    return {
        initializeCustomerAccountResult: function() {
        var params = "xyz";
        var promise = customerService.getCustomerBookings(params)
                .then(function(bookingResponse) {
                    var customerBookings = constructCustomerBookings(bookingResponse);
                    return customerBookings;
                });
            return promise;
    } };
    
  3. CustomerBookingController.js

    function CustomerBookingController(customerBookingListModel,customerService???) {
    $scope.LoadMoreBookings = function(params) {
        var bookings = customerService.getCustomerBookings(params);
    };
    

0 个答案:

没有答案