在我们的一个项目中,我们试图围绕ngTable库编写一个包装器!在此组件的控制器中,我们使用ngTable重新加载ocLazyLoad。 这是我们写的:
(function() {
'use strict';
angular
.module('CommonApplication')
.component('datagrid', {
scope: true,
bindings: {
basepath: '@',
title: '@',
dataset: '<'
},
templateUrl: '/frontend/components/global/datagrid/templates/datagrid.html',
controller: 'DataGridController',
});
})();
(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
});
});
}
})();
(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
中有这个特殊错误{1}}属性。
这里是异常错误,它指向$ocLazyLoad.then
函数回调的结束!它是datagrid.js:47:7
因为我们将这些js文件和另一个文件连接到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
对此有何想法?!
答案 0 :(得分:1)
显然,即使使用其承诺.then(callback)
,延迟加载也不会使Angular等待其完成!
我正在做的是在加载ngTableParams
时使用ng-table
初始化表格!考虑到这一点,我决定使用一个简单的技巧!
我将ngTableParams附加到名为vm.tableParams
的Scope变量中,稍后我将使用ng-table
指令将其初始化为视图中的表。因此,即使在调用尚未加载的指令之前,我们仍然使用ng-if
和tableParams
检查延迟加载是否已完成:
// 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]}}
// 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处理编译本身,而不影响性能:)