Javascript 如何检测调用递归函数的位置
在尝试为我的eshop实现购物车时,我注意到initCartId和refreshCart被递归调用,导致项目崩溃 问题与相同,但这不能帮助我确定在哪里使用递归 当我转到购物车页面时,controller.js发出的警报如下:Javascript 如何检测调用递归函数的位置,javascript,jquery,recursion,Javascript,Jquery,Recursion,在尝试为我的eshop实现购物车时,我注意到initCartId和refreshCart被递归调用,导致项目崩溃 问题与相同,但这不能帮助我确定在哪里使用递归 当我转到购物车页面时,controller.js发出的警报如下: initCartId!!! refreshCart!!! calGrandTotal!!! calGrandTotal!!! calGrandTotal!!! calGrandTotal函数的前两个警报如下所示: 此时,我可以在控制台上看到StackOverflower
initCartId!!!
refreshCart!!!
calGrandTotal!!!
calGrandTotal!!!
calGrandTotal!!!
calGrandTotal函数的前两个警报如下所示:
此时,我可以在控制台上看到StackOverflower错误,但警报会按照以下顺序再次出现:
initCartId!!!
refreshCart!!!
calGrandTotal!!!
cart.jsp
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@include file="/WEB-INF/views/template/header.jsp"%>
<div class="container-wrapper">
<div class="container">
<section>
<div class="jumbotron">
<div class="container">
<h1>Cart</h1>
<p>All the selected products in your shopping cart</p>
</div>
</div>
</section>
<section class="container" ng-app="cartApp">
<div ng-controller="cartCtrl" ng-init="initCartId('${cartId}')">
<div>
<a class="btn btn-danger pull-left" ng-click="clearCart()"><span
class="glyphicon glyphicon-remove-sign"></span>Clear Cart</a>
</div>
<table class="table table-hover">
<tr>
<th>Product</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Price</th>
<th>Action</th>
</tr>
<tr ng-repeat="item in cart.cartItems">
<td>{{item.product.productName}}</td>
<td>{{item.product.productPrice}}</td>
<td>{{item.quantity}}</td>
<td>{{item.totalPrice}}</td>
<td><a href="#" class="label label-danger"
ng-click="removeFromCart(item.product.productId)"> <span
class="glyphicon glyphicon-remove"></span>remove
</a></td>
</tr>
<!-- <tr>
<th></th>
<th></th>
<th>Grand Total</th>
<th>{{calGrandTotal()}}</th>
<th></th>
</tr> -->
</table>
<a href="<spring:url value="/productList" />"
class="btn btn-default">Continue Shopping</a>
</div>
</section>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script src="<c:url value="/resources/js/controller.js" /> "></script>
<%@include file="/WEB-INF/views/template/footer.jsp"%>
运货马车
您购物车中的所有选定产品
controller.js
var cartApp = angular.module ("cartApp", []);
cartApp.controller("cartCtrl", function ($scope, $http){
/* corresponding to the mapper at CartController with RequestMethod.GET */
/*
* /eMusicStore/rest/cart/'+$scope.cartId) get the cart data in Json format
* and with the success we use another function and the JSON format cart
* info will be stored into data and we pass into $scope
*/
$scope.refreshCart = function (cartId) {
alert("refreshCart!!!");
$http.get('/eMusicStore/rest/cart/'+$scope.cartId).success(function (data) {
$scope.cart=data;
});
};
$scope.clearCart = function () {
alert("clearCart!!!");
/*$http.delete('/eMusicStore/rest/cart/'+$scope.cartId).success($scope.refreshCart($scope.cartId));*/
$http.delete('/eMusicStore/rest/cart/'+$scope.cartId).success(function (data) {
$scope.refreshCart($scope.cartId);
});
};
$scope.initCartId = function (cartId) {
alert("initCartId!!!");
$scope.cartId = cartId;
/* alert("cartId:" + cartId);*/
$scope.refreshCart($scope.cartId);
};
$scope.addToCart = function (productId) {
alert("addToCart!!!");
$http.put('/eMusicStore/rest/cart/add/'+productId).success(function (data) {
/*$scope.refreshCart($http.get('/eMusicStore/rest/cart/cartId'));*/
alert("Product successfully added to the cart!");
});
};
$scope.removeFromCart = function (productId) {
alert("removeFromCart!!!");
$http.put('/eMusicStore/rest/cart/remove/'+productId).success(function (data) {
/*$scope.refreshCart($http.get('/eMusicStore/rest/cart/cartId'));*/
$scope.refreshCart($scope.cartId);
});
};
$scope.calGrandTotal = function () {
alert("calGrandTotal!!!");
var grandTotal=0;
for (var i=0; i<$scope.cart.cartItems.length; i++) {
grandTotal+=$scope.cart.cartItems[i].totalPrice;
}
return grandTotal;
};
});
var cartApp=angular.module(“cartApp”,[]);
控制器(“cartCtrl”,函数($scope,$http){
/*对应于CartController上带有RequestMethod.GET的映射程序*/
/*
*/eMusicStore/rest/cart/'+$scope.cartId)以Json格式获取购物车数据
*我们成功地使用了另一个函数和JSON格式的购物车
*信息将存储到数据中,我们将其传递到$scope中
*/
$scope.refreshCart=函数(cartId){
警报(“刷新购物车!!!”;
$http.get('/eMusicStore/rest/cart/'+$scope.cartId).success(函数(数据){
$scope.cart=数据;
});
};
$scope.clearCart=函数(){
警报(“clearCart!!!”;
/*$http.delete('/eMusicStore/rest/cart/'+$scope.cartId).success($scope.refreshCart($scope.cartId));*/
$http.delete('/eMusicStore/rest/cart/'+$scope.cartId).成功(函数(数据){
$scope.refreshCart($scope.cartId);
});
};
$scope.initCartId=函数(cartId){
警报(“initCartId!!!”;
$scope.cartId=cartId;
/*警报(“cartId:+cartId”)*/
$scope.refreshCart($scope.cartId);
};
$scope.addToCart=函数(productId){
警报(“addToCart!!!”;
$http.put('/eMusicStore/rest/cart/add/'+productId).success(函数(数据){
/*$scope.refreshCart($http.get('/eMusicStore/rest/cart/cartId')*/
警报(“产品已成功添加到购物车!”);
});
};
$scope.removeFromCart=函数(productId){
警报(“removeFromCart!!!”;
$http.put('/eMusicStore/rest/cart/remove/'+productId).成功(函数(数据){
/*$scope.refreshCart($http.get('/eMusicStore/rest/cart/cartId')*/
$scope.refreshCart($scope.cartId);
});
};
$scope.calGrandTotal=函数(){
警报(“calGrandTotal!!!”;
var-grandTotal=0;
对于(var i=0;我在代码中看不到任何递归调用。这也是我的问题。你的意思是递归可能会在我的项目中的其他地方触发吗?我不知道AngularJS
(你应该在标记中包含它)但是在你的refreshCart
中,你说$scope.cart=data;
这对我来说意味着我们有了一个新的购物车,所以initCartId
应该再次运行?也许如果你把$scope.cart=data;
改成$scope.cart.cartims=data.cartims;
,这是个很好的主意,但不能解决我的问题。我不明白为什么函数initCartId被调用了2次..我试图删除所有的refreshCart函数,但是initCartId仍然被调用了2次..但是如何以及为什么?