使用 ocLazyLoad 延迟加载表

lazyloading ngTable using ocLazyLoad

在我们的一个项目中,我们试图围绕 ngTable 库编写一个包装器!在此组件的控制器中,我们正在加载 ngTable using ocLazyLoad。 这是我们写的:

Datagrid 组件

(function() {

  'use strict';

  angular
    .module('CommonApplication')
    .component('datagrid', {
      scope: true,
      bindings: {
        basepath: '@',
        title: '@',
        dataset: '<'
      },
      templateUrl: '/frontend/components/global/datagrid/templates/datagrid.html',
      controller: 'DataGridController',
    });

})();

带有 $ocLazyLoad 的 Datagrid 控制器(不起作用 :~| )

(function() {

  'use strict';

  angular
    .module('CommonApplication')
    .controller('DataGridController', 
    [ '$scope', '$injector', '$ocLazyLoad', DataGridController ]);

  function DataGridController($scope, $injector, $ocLazyLoad) {

    var vm = this;
    var components = {
      datagrid: [
        'https://unpkg.com/ng-table@2.2.0/bundles/ng-table.min.css',
        'https://unpkg.com/ng-table@2.2.0/bundles/ng-table.min.js'
      ]
    };

    $ocLazyLoad.load(components.datagrid).then(function() {
      var NgTableParams = $injector.get('NgTableParams');
      vm.data = [{name: "Moroni", age: 50},{name: "Moroni", age: 50},{name: "Moroni", age: 50},{name: "Moroni", age: 50}];
      vm.tableParams = new NgTableParams({
        page: 1,
        count: 10
      }, {
        dataset: vm.data
      });
    });
  }

})();

没有 $ocLazyLoad 的 Datagrid 控制器(有效)

(function() {

  'use strict';

  angular
    .module('CommonApplication')
    .controller('DataGridController', 
    [ '$scope', '$injector', '$ocLazyLoad', DataGridController ]);

  function DataGridController($scope, $injector, $ocLazyLoad) {

    var vm = this;
    var components = {
      datagrid: [
        'https://unpkg.com/ng-table@2.2.0/bundles/ng-table.min.css',
        'https://unpkg.com/ng-table@2.2.0/bundles/ng-table.min.js'
      ]
    };

    var NgTableParams = $injector.get('NgTableParams');
    vm.data = [{name: "Moroni", age: 50},{name: "Moroni", age: 50},{name: "Moroni", age: 50},{name: "Moroni", age: 50}];
    vm.tableParams = new NgTableParams({
      page: 1,
      count: 10
    }, {
      dataset: vm.data
    });
  }

})();

使用 ocLazyLoad,如果我在定义 vm.tableParams 的行上设置一个断点,当它被 new NgTableParams 填充时,它的设置和其他属性在它们的 arguments属性。


这是指向 $ocLazyLoad.then 函数回调结束的异常错误!这是 datagrid.js:47:7 因为我们将这些 js 文件和另一个 concat 成 datagrid.js:

TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
    at Function.remoteFunction (<anonymous>:3:14)
    at http://localhost:8081/frontend/components/global/datagrid/datagrid.js:47:7
    at http://localhost:8081/frontend/javascripts/head.min.js:136:20
    at m.$eval (http://localhost:8081/frontend/javascripts/head.min.js:150:347)
    at m.$digest (http://localhost:8081/frontend/javascripts/head.min.js:147:420)
    at http://localhost:8081/frontend/javascripts/head.min.js:150:434
    at e (http://localhost:8081/frontend/javascripts/head.min.js:50:444)
    at http://localhost:8081/frontend/javascripts/head.min.js:53:300

对此有什么想法吗?!

显然,延迟加载即使使用它的承诺 .then(callback) 也不会让 Angular 等待它完成!

我所做的是在 ng-table 加载完成时使用 ngTableParams 初始化 table!考虑到这一点,我决定使用一个简单的技巧!

我将 ngTableParams 附加到一个名为 vm.tableParams 的范围变量,稍后我将在使用 ng-table 的指令时使用它来初始化视图中的 table。因此,即使在调用尚未加载的指令之前,我们也会使用 ng-iftableParams:

检查延迟加载是否完成

查看:datagrid.pug

// Load Directive if Params in the controller were attached to the scope!
.row.expanded(ng-if="table.tableParams")
    .small-12.columns 
      table.data-table.hover.stacktable.table-condensed.table-bordered.table-striped(ng-table-dynamic="table.tableParams with table.headers",template-pagination="custom/pager", show-filter="true")
        tr(ng-repeat="(rowIndex, row) in $data ")
          td(ng-repeat="column in $columns", ng-switch="column.field")
            input.selectSwitch( ng-switch-when="selector", type="checkbox", ng-model="table.checkboxes.items[row.id]") 
            div(ng-switch-when="id") {{ rowIndex + 1 }}
            div(ng-switch-when="body") {{ row }}
            span(ng-switch-default) {{row[column.field]}}

控制器:datagrid.controller.js

// Lazyload datagrid components and Initialize headers and parametres!
    $ocLazyLoad.load(components.datagrid).then(function (){
      self.headers = self.dataset.headers;
      self.dataset.headers.forEach(function( header ) {
        header.title = header.headerTitle;
      });
      var NgTableParams = $injector.get('NgTableParams');
      self.tableParams = new NgTableParams({
        count: 5
      }, {
        dataset: self.dataset.data
      });
    });

这样我们就不会使用 $compile 手动重新渲染视图,我们让 angular 自己处理编译,不会影响性能 :)