无法从数据集绑定到 ngTable
Unable to bind to ngTable from dataset
所以在 Angular 中,我尝试将 ng-Table 绑定到数据集,但似乎无法处理我的响应数据。我想我的问题出在这里的某个地方,
""-- self.tableParams = new NgTableParams({}, { dataset: 'data' }); --""
但我不确定。另外,我没有收到任何错误控制台。
有没有人能给点启发,谢谢。
控制器:
UtilityApp.controller('u_CompaniesController', function ($scope, $http, NgTableParams) {
$scope.message = "Companies";
$scope.myData = [];
$http({
method: 'GET',
url: '/Utility/GetCompanies',
}).then(function successCallback(response) {
var self = this;
var data = response.data;
self.tableParams = new NgTableParams({}, { dataset: 'data' });
}, function errorCallback(response) {
alert("error");
});
});
吴-Table
<div ng-controller="u_CompaniesController">
<table id="tblCompanies" ng-table="vm.tableParams" class="table" show-filter="true">
<tr ng-repeat="c in $tableParams">
<td title="'CompanyCd'" filter="{ CompanyCd: 'text'}" sortable="'CompanyCd'">
{{c.CompanyCd}}
</td>
<td title="'CompanyName'" filter="{ CompanyName: 'text'}" sortable="'CompanyName'">
{{c.CompanyName}}
</td>
</tr>
</table>
</div>
尝试将 var self = this
移到控制器声明的右侧。您还应该将 html 更新为 ng-controller="u_CompaniesController as vm"
。阅读 this blogpost 有关 Controller as Syntax 的更多信息。
或者只使用 $scope
,如果您刚开始使用 angular,我会推荐它。在这种情况下,从 html ng-table="tableParams"
中删除 vm
$scope.tableParams = new NgTableParams({}, { dataset: 'data' });
控制器
UtilityApp.controller('u_CompaniesController', function ($scope, $http, NgTableParams) {
$scope.message = "Companies";
$scope.myData = [];
$http({
method: 'GET',
url: '/Utility/GetCompanies',
}).then(function successCallback(response) {
var data = response.data;
$scope.tableParams = new NgTableParams({}, { dataset: 'data' });
}, function errorCallback(response) {
alert("error");
});
HTML
<div ng-controller="u_CompaniesController">
<table id="tblCompanies" ng-table="tableParams" class="table" show-filter="true">
<tr ng-repeat="c in $tableParams">
<td title="'CompanyCd'" filter="{ CompanyCd: 'text'}" sortable="'CompanyCd'">
{{c.CompanyCd}}
</td>
<td title="'CompanyName'" filter="{ CompanyName: 'text'}" sortable="'CompanyName'">
{{c.CompanyName}}
</td>
</tr>
</table>
</div>
所以在 Angular 中,我尝试将 ng-Table 绑定到数据集,但似乎无法处理我的响应数据。我想我的问题出在这里的某个地方, ""-- self.tableParams = new NgTableParams({}, { dataset: 'data' }); --"" 但我不确定。另外,我没有收到任何错误控制台。 有没有人能给点启发,谢谢。
控制器:
UtilityApp.controller('u_CompaniesController', function ($scope, $http, NgTableParams) {
$scope.message = "Companies";
$scope.myData = [];
$http({
method: 'GET',
url: '/Utility/GetCompanies',
}).then(function successCallback(response) {
var self = this;
var data = response.data;
self.tableParams = new NgTableParams({}, { dataset: 'data' });
}, function errorCallback(response) {
alert("error");
});
});
吴-Table
<div ng-controller="u_CompaniesController">
<table id="tblCompanies" ng-table="vm.tableParams" class="table" show-filter="true">
<tr ng-repeat="c in $tableParams">
<td title="'CompanyCd'" filter="{ CompanyCd: 'text'}" sortable="'CompanyCd'">
{{c.CompanyCd}}
</td>
<td title="'CompanyName'" filter="{ CompanyName: 'text'}" sortable="'CompanyName'">
{{c.CompanyName}}
</td>
</tr>
</table>
</div>
尝试将 var self = this
移到控制器声明的右侧。您还应该将 html 更新为 ng-controller="u_CompaniesController as vm"
。阅读 this blogpost 有关 Controller as Syntax 的更多信息。
或者只使用 $scope
,如果您刚开始使用 angular,我会推荐它。在这种情况下,从 html ng-table="tableParams"
vm
$scope.tableParams = new NgTableParams({}, { dataset: 'data' });
控制器
UtilityApp.controller('u_CompaniesController', function ($scope, $http, NgTableParams) {
$scope.message = "Companies";
$scope.myData = [];
$http({
method: 'GET',
url: '/Utility/GetCompanies',
}).then(function successCallback(response) {
var data = response.data;
$scope.tableParams = new NgTableParams({}, { dataset: 'data' });
}, function errorCallback(response) {
alert("error");
});
HTML
<div ng-controller="u_CompaniesController">
<table id="tblCompanies" ng-table="tableParams" class="table" show-filter="true">
<tr ng-repeat="c in $tableParams">
<td title="'CompanyCd'" filter="{ CompanyCd: 'text'}" sortable="'CompanyCd'">
{{c.CompanyCd}}
</td>
<td title="'CompanyName'" filter="{ CompanyName: 'text'}" sortable="'CompanyName'">
{{c.CompanyName}}
</td>
</tr>
</table>
</div>