包含在mysql数据库页面数据中的fetch.php的结果
[{“ id”:“ 1”,“ name”:“移动电话”,“ price”:“ 3000”,“ image”:“ mobil1”,“ desc”:“ samasong mobile garand prime 2+” },{“ id”:“ 2”,“名称”:“手表”,“价格”:“ 200”,“图像”:“手表”,“ desc”:“现代手表,其颜色为金色”},{ “ id”:“ 3”,“ name”:“ Labtop”,“ price”:“ 4000”,“ image”:“ labtop”,“ desc”:“ hp labtop core i5 space 500gb”},{“ id” :“ 4”,“名称”:“ molite lite”,“ price”:“ 1999”,“ image”:“ mobil2”,“ desc”:“ moble lite sterrr”}]
此html代码
<!DOCTYPE html>
<html>
<head>
<title>shoping cart</title>
<link rel="stylesheet" type="text/css" href="functions/bootstrap.css">
<script type="text/javascript" src="functions/jquery.js"></script>
<script type="text/javascript" src="functions/bootstrap.min.js"></script>
<script type="text/javascript" src="functions/angular.min.js"></script>
</head>
<body>
<h2 align="center">Shopping cart application by angularjs and php</h2>
<div class="container" ng-app="shoppingcart" ng-controller="shoppingcartcontroller">
<div class="row " >
<div class="box col-md-3" style="margin-top: 20px"
ng-repeat="p in products">
{{p.name}}
{{p.price}}
</div>
</div>
</div>
此angularjs代码
<script >
var app=angular.module("shoppingcart",[]);
app.controller("shoppingcartcontroller",function($http,$scope){
$scope.loadproduct=function(){
$http.get("fetch.php").then(function(response){
$scope.products=response.data.data;
console.log($scope.products);
});
};
});
</script>
答案 0 :(得分:0)
为什么需要这个loadproduct函数?
请仅在您的控制器中使用此
: $http.get("fetch.php").then(function(response){
$scope.products=response.data.data;
console.log($scope.products);
});
此外,请考虑是否需要将'$ scope.products'定义为get请求范围之外的空数组。