从角度js控制器初始化数据在数据表中
问题描述:
我正在面对来自Angular控制器的Jquery数据表中的数据初始化问题。请找到下面的方法。 我一个角控制器:从角度js控制器初始化数据在数据表中
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()
来初始化数据,它加载数据但其他功能初始化消失。
请帮忙。 谢谢 Jitender
答
我在你的页面看到jquery ui。尝试理解jqui的行为并在指令中处理此代码。
嗨,感谢您的回应,实际上我认为jqui部分没有问题,因为它是将功能添加到数据表,并且按预期工作,我主要关心的是我想初始化我的公司变量控制器从服务方法,我不能做一个功能,例如self.companies = angular.toJson(companyService.fetchAllCompanies());类似的东西,我根本无法做到,如果我实现了这一点,那么它将工作,服务方法fetchAllComapnies没有返回在这个控制器的实际响应。 – Jitender