我在一个简单的应用程序中使用angular,如:
app.js
app = angular.module('app', []);
app.controller("NavCtrl",function($scope,$http){
var serviceBase = 'v1/';
$http.get(serviceBase + 'orderoverview/58').then(function (results) {
$scope.categories = results.data;
$scope.getTotal = function(){
var total = 0;
for(var i = 0; i < $scope.categories.length; i++){
var categories = $scope.categories[i];
total += (categories.productPrice * categories.orderdetailAantal);
if(categories.extra1 != "")
{
total += categories.extrasPrice;
}
}
return total;
}
});
});
HTML
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="utf-8">
<title>Served</title>
</head>
<body ng-cloak="">
<div class="navbar navbar-default megamenu">
<div class="container" ng-controller="NavCtrl">
<ul class="nav navbar-nav">
<li ng-repeat= "p in categories">{{p.orderdetailAantal}} x {{p.productTitle}} {{p.productPrice}} <br> {{p.extra1}} {{p.extra2}} {{p.extra3}} {{p.extra4}} {{p.extrasPrice}}</li>
</ul>
<p>Total: {{ getTotal() }} euro</p>
</div>
</div>
<script src="libs/angular.js"></script>
<script src="app/app.js"></script>
</body>
</html>
这样可行,但每次加载页面时都会有几乎3秒的延迟从数据库中获取数据并在我的html页面中显示... 当我直接浏览API时,我使用PHP构建,所需的数据立即显示...
这怎么可能?如何加快这个过程呢?
(如果需要,我的API和.htaccess)
API
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();
$app->get('/orderoverview/:customerID', function ($customerID) {
$sql = "SELECT `customerName` FROM `customers` WHERE `customerID` = $customerID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$customername = $row["customerName"];
}
}
else {
echo "Customer name failure";
}
$sql = "SELECT `orderID` FROM `order` WHERE `customerID` = $customerID";
$result = $conn->query($sql);
if($result->num_rows > 1){
echo "You already have a pending order! <br>";
}
else if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$orderID = $row["orderID"];
}
step2($orderID, $customername);
} else {
echo "GET orderID failure";
}
});
$app->run();
function step2($orderID, $customername){
mysql_connect($servername, $username, $password);
mysql_select_db($dbname);
$query = mysql_query("SELECT `orderdetailID` FROM `orderdetail` WHERE `orderID` = '$orderID'");
$results = array();
while($line = mysql_fetch_array($query, MYSQL_ASSOC)){
$results[] = $line;
}
step3($orderID, $results, $customername);
}
function step3($orderID, $results, $customername){
global $conn, $servername, $username, $password, $dbname;
$to = 0;
$extracounter = 1;
$extrasprice = 0;
foreach($results as $key=>$odt)
{
$extracounter = 1;
$odb = $odt["orderdetailID"];
$sql = "SELECT `productID`, `orderdetailAantal` FROM `orderdetail` WHERE `orderdetailID` = '$odb'";
$sql2 = "SELECT `extraID` FROM `orderdetailextra` WHERE `orderdetailID` = '$odb'";
$result = $conn->query($sql);
$result2 = $conn->query($sql2);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$sql3 = "SELECT `productTitle` FROM `products` WHERE `productID` = '$row[productID]'";
$sql4 = "SELECT `productPrijs` FROM `products` WHERE `productID` = '$row[productID]'";
$result3 = $conn->query($sql3);
if($result3->num_rows > 0) {
while($row1 = $result3->fetch_assoc()){
$productTitle = $row1["productTitle"];
}
}
$result4 = $conn->query($sql4);
if($result4->num_rows > 0) {
while($row1 = $result4->fetch_assoc()){
$productPrice = $row1["productPrijs"];
}
}
$order[$to] = array(
"orderdetailID" => $odb,
"productTitle" => $productTitle,
"productPrice" => $productPrice,
"orderdetailAantal" => $row["orderdetailAantal"],
"extra1" => "",
"extra2" => "",
"extra3" => "",
"extra4" => "",
"extrasPrice" => ""
);
$to++;
}
} else {
echo 'fout sql 1';
}
}
}
}
}
echoResponse(200, $order);
}
function echoResponse($status_code, $response) {
global $app;
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response,JSON_NUMERIC_CHECK);
}
的.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
RewriteBase /localsites/serf/weap/api/v1