【发布时间】:2017-03-31 12:01:36
【问题描述】:
我在来自 Angular 控制器的 Jquery 数据表中面临数据初始化问题。请在下面找到方法。 我有一个角度控制器:
app.controller('CompanyController', ['$scope', 'CompanyService',function($scope, companyService) {
var self = this;
self.companies=[];
self.fetchAllCompanies = function() {
companyService.fetchAllCompanies().then(function(d) {
self.companies = d;
},
function(errResponse) {
console.error('Error while fetching Compnies');
});
};
//self.fetchAllCompanies();
$scope.dataTableOpt = {
"aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
"searching": true
};
}]);
HTML 代码是:
<div class="row" ng-controller="CompanyController as ctrl">
<table id="company-details" class="table table-bordered table-striped" ui-jq="dataTable" ui-options="dataTableOpt">
<thead>
<tr>
<th>Company Name</th>
<th class="text-center">Created Date</th>
<th class="text-center">Modified Date</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="company in ctrl.companies">
<td>{{company.companyName}}</td>
<td class="text-center">{{company.createdDate | date:'MMM dd, yyyy'}}</td>
<td class="text-center">{{company.modifiedDate | date:'MMM dd, yyyy'}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Company Name</th>
<th class="text-center">Created Date</th>
<th class="text-center">Modified Date</th>
</tr>
</tfoot>
</table></div>
我正在使用角度数据表,这按预期工作,但有一个问题:
如果我从我的控制器中取消注释行 //self.fetchAllCompanies(); 则数据表加载数据但在使用一些排序数据搜索时消失了。
但是如果我在控制器中将硬编码的 json 分配给 self.companies=[]; 则数据表一直工作,现在的问题是我正在通过调用方法 self.fetchAllCompanies() 来初始化数据,它会加载数据但其他功能初始化消失了。
请帮忙。 谢谢 抖动
【问题讨论】: