Angularjs 获取无法读取属性';然后';在角度数据表中使用.fromfnomise时的未定义值
当我在那个视图中时,它抛出这个错误,不能读取未定义的属性“then”。以下是这两个来源的例子Angularjs 获取无法读取属性';然后';在角度数据表中使用.fromfnomise时的未定义值,angularjs,angular-services,Angularjs,Angular Services,当我在那个视图中时,它抛出这个错误,不能读取未定义的属性“then”。以下是这两个来源的例子 您需要从loadAdmin方法返回promise对象(ApiService.get(“admin”)调用的结果) 还要确保不要“吞下”然后(在console.log分支中)内部的拒绝-当您无意中通过不进一步传递错误来处理错误时会发生什么情况。对于此返回,拒绝承诺或只是抛出错误,因此拒绝将沿着承诺链进一步传播: //the controller that creates the datatable app
您需要从
loadAdmin
方法返回promise对象(ApiService.get(“admin”)调用的结果)
还要确保不要“吞下”然后(在console.log
分支中)内部的拒绝-当您无意中通过不进一步传递错误来处理错误时会发生什么情况。对于此返回,拒绝承诺或只是抛出错误,因此拒绝将沿着承诺链进一步传播:
//the controller that creates the datatable
app.controller('AdminListCtrl', function ($scope, $compile, DTOptionsBuilder, DTColumnBuilder, adminService) {
var vm = this;
function stateChange(iColumn, bVisible) {
console.log('The column', iColumn, ' has changed its status to', bVisible);
}
//vm.dtOptions = DTOptionsBuilder.fromSource('http://localhost/api-v1/admin')
vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() {
return adminService.loadAdmin();
})
.withPaginationType('full_numbers')
.withOption('createdRow', createdRow)
// Add Bootstrap compatibility
.withBootstrap()
// Active ColVis plugin
.withColVis()
// Add a state change function
.withColVisStateChange(stateChange)
// Exclude the last column from the list
.withColVisOption('aiExclude', [2])
// Add Table tools compatibility
.withTableTools('scripts/vendor/datatables/TableTools/swf/copy_csv_xls_pdf.swf')
.withTableToolsButtons([
'copy',
'print', {
'sExtends': 'collection',
'sButtonText': 'Save',
'aButtons': ['csv', 'xls', 'pdf']
}
]);
//adminService to request for all administrators
app.factory('adminService', ['ApiService', function (ApiService) {
return {
loadAdmin: function () {
ApiService.get("admin").then(function (response) {
if (response) {
if (response.success === true) {
return response;
}else{
console.log(response);
}
}else {
console.log('error request ');
}
});
}
};
}]);
//apiservice to interact with api
app.factory('ApiService', function ($http, $q, $localStorage) {
return {
get: function (apiresource) {
var returnData = $q.defer();
$http({
url: api + apiresource,
method: 'GET',
headers: {"Auth-Token": $localStorage.user_data.auth_token}
})
.success(function (data) {
returnData.resolve(data);
})
.error(function (error) {
returnData.resolve();
});
return returnData.promise;
}};
});`enter code here`
app.factory('adminService', ['ApiService', function (ApiService) {
return {
loadAdmin: function () {
return ApiService.get("admin").then(function (response) {
if (response) {
if (response.success === true) {
return response;
} else{
console.log(response);
throw response;
// or custom error object: throw {message: 'Error loadAdmin', response}
}
} else {
console.log('error request ');
throw new Error('error request');
}
});
}
};
}]);