使用angularjs从promise服务器获取数据
我如何从db获取表,并承诺。我已经创建了一个带有promise的服务和http调用。我已经检查了控制台,没有调用url。我不确定这是创建服务返回http承诺的八种方法使用angularjs从promise服务器获取数据,angularjs,Angularjs,我如何从db获取表,并承诺。我已经创建了一个带有promise的服务和http调用。我已经检查了控制台,没有调用url。我不确定这是创建服务返回http承诺的八种方法 <!doctype html> <html lang="en" ng-app="myApp"> <head> <meta charset="utf-8"> <base href="/"> <title>T
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<base href="/">
<title>The Single Page Blogger</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script data-require="ui-bootstrap@*" data-semver="0.12.1" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
<script src="<%=request.getContextPath()%>/js/module.js"></script>
<link rel="stylesheet" href="<%=request.getContextPath()%>/style2.css" />
<script>
app.controller('tableController', function ($log, $scope, tableService)
{
$scope.customerTable = [];
var promise = tableService.fetchTable();
promise.then(function (data)
{
console.log("Your name is: " + data);
$scope.customerTable = data;
});
});
app.factory('tableService', function ($http)
{
return
{
fetchTable: function()
{
return $http.get('<%=request.getContextPath()%>/GetTable.do');
}
}
});
</script>
</head>
<body>
<div class="container" id="main"><br/><br/>
Search: <input type="text" ng-model="search" placeholder="Search">
<div ng-controller="tableController">
<table class="table table-striped table-hover table-bordered">
<tr>
<th style="font-size: 13.3px">Card number</th>
<th style="font-size: 13.3px">First name</th>
<th style="font-size: 13.3px">Opening balance</th>
<th style="font-size: 13.3px">Withdrawal</th>
<th style="font-size: 13.3px">Deposit</th>
<th style="font-size: 13.3px">Closing balance</th>
<th style="font-size: 13.3px">Tx date</th>
<th style="font-size: 13.3px">Usage type</th>
</tr>
<tr ng-repeat="data in filteredTodos| filter: search">
<td>{{data.CARD_NUMBER}}</td>
<td>{{data.FIRST_NAME}}</td>
<td>{{data.OPENING_BALANCE}}</td>
<td>{{data.WITHDRAWAL}}</td>
<td>{{data.DEPOSIT}}</td>
<td>{{data.CLOSING_BAL}}</td>
<td>{{data.TXDATE}}</td>
<td>{{data.USAGE_TYPE}}</td>
</tr>
</table>
<pagination
ng-model="currentPage"
total-items="customerTable.length"
max-size="maxSize"
boundary-links="true">
</pagination>
<br/><br/><br>
<button class="form-control btn btn-success" type="submit" ng-click="fetchTable()">Load Table Data</button>
</div>
</div>
</body>
</html>
单页博客
app.controller('tableController',函数($log,$scope,tableService)
{
$scope.customerTable=[];
var promise=tableService.fetchTable();
promise.then(函数(数据)
{
log(“您的名字是:“+data”);
$scope.customerTable=数据;
});
});
app.factory('tableService',函数($http)
{
返回
{
fetchTable:function()
{
返回$http.get('/GetTable.do');
}
}
});
搜索:
卡号
名字
期初余额
撤回
押金
期终余额
发送日期
使用类型
{{data.CARD_NUMBER}
{{data.FIRST{u NAME}
{{data.OPENING{u BALANCE}}
{{data.drawing}
{{data.DEPOSIT}}
{{data.CLOSING{u BAL}}
{{data.TXDATE}
{{data.USAGE{u TYPE}
加载表数据
这应该可以做到。您在设置
$scope.customerTable
之前返回了数据。事实上,你为什么还要返回数据呢?你不能从那样的回调中返回任何东西。我也删除了该行。您的服务做出了承诺,但没有实现,请尝试将$q服务添加到您的服务中,如下所示:
app.factory('tableService', function ($http, $q) {
return {
fetchTable: function() {
// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default
return $http.get('<%=request.getContextPath()%>/GetTable.do')
.then(function(response) {
if (typeof response.data === 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
};
});
app.controller('tableController', function ($log, $scope, tableService)
{
$scope.customerTable = [];
$scope.getData = function() {
var promise = tableService.fetchTable();
promise.then(function (data)
{
console.log("Your name is: " + data);
$scope.customerTable = data;
});
}
});
<div ng-controller="tableController" ng-init="getData()">
app.factory('tableService',函数($http,$q){
返回{
fetchTable:function(){
//$http API基于$q服务公开的延迟/承诺API
//因此,默认情况下,它会为我们返回一个承诺
返回$http.get('/GetTable.do')
.然后(功能(响应){
if(type of response.data==='object'){
返回响应数据;
}否则{
//无效响应
返回$q.reject(response.data);
}
},功能(回应){
//出了点问题
返回$q.reject(response.data);
});
}
};
});
app.controller('tableController',函数($log,$scope,tableService)
{
$scope.customerTable=[];
$scope.getData=function(){
var promise=tableService.fetchTable();
promise.then(函数(数据)
{
log(“您的名字是:“+data”);
$scope.customerTable=数据;
});
}
});
http url未被调用。正如你所说,我更改了代码。我写的服务正确吗?@kittu在返回$http.get之前,你能放一个控制台日志吗?只是为了确保函数本身正确运行。尝试在then()之后添加一个.catch,并记录错误,看看它是否有任何含义。例如:catch(function(error){console.log(“发生了一个错误:+error”);
?fetchTable函数运行了吗?尝试了吗?没有控制器吗?我如何在html中调用它?只需调用fetchTable()
单击按钮?尝试此操作:加载表数据
仍然不工作您的控制器保持不变,尽管我会将您的服务调用添加到方法中,并在div上调用ng init。您不需要像他一样返回$q服务。仅当您希望手动创建延迟承诺并挂接解析和但应该可以工作,尤其是使用init,这样他的代码就不会在创建控制器时执行
app.factory('tableService', function ($http, $q) {
return {
fetchTable: function() {
// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default
return $http.get('<%=request.getContextPath()%>/GetTable.do')
.then(function(response) {
if (typeof response.data === 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
};
});
app.controller('tableController', function ($log, $scope, tableService)
{
$scope.customerTable = [];
$scope.getData = function() {
var promise = tableService.fetchTable();
promise.then(function (data)
{
console.log("Your name is: " + data);
$scope.customerTable = data;
});
}
});
<div ng-controller="tableController" ng-init="getData()">