I have built a single page app using angularJS routing . I have a cart page where a user has selected products and can proceed to checkout .
If you click on the button you get redirected to the checkout page .
The problem is that I have the total cost hard coded and I want to pass it from the product page to the checkout page using jquery or ajax . I can obviously use localstorage but If I switch back to my product page and edit the price and then return to checkout ,since no reload happens localstorage shows the previous total . This is why I need a jquery or ajax solution .
My code :
//-----Click Checkout button ------
$("#checkOut").click(()=>{
//get count of products
var numOfProducts = $('.product-columns').length;
if(numOfProducts!=0){
//pass total cost to variable
var total = $('.FinalTotal').text();
//append total cost to html total cost element of checkout.php
window.location.href='../php/index.php#!/purchase';
}else{
alert("Your cart is empty");
}
});
total from products.php
<p> Total </p>
<span class="new__price FinalTotal">$0</span>
<a href="" id ="checkOut">PROCEED TO CHECKOUT</a>
purchase.php
<h2 id = "totalPrice"> Total Price : </h2>
angularjs routing for a single page app
app.config(($routeProvider)=>{
$routeProvider
.when("/" , {templateUrl:"main.php"})
.when("/login" , {templateUrl : "login.php"})
.when("/register" , {templateUrl : "register.php"})
.when("/results" , {templateUrl : "showResults.php"})
.when("/purchase", {templateUrl:"purchase.php"})
.when("/cart" ,{templateUrl:"products.php"});
});
2
Answers
This is what I did to make this work using localstorage and ajax :
There are multiple ways of passing data between your pages :
Since you are already using $routeProvider, there’s no need to navigate using
window.location.href,
you can use$location.path.
Now, when routing to your purchase page:
In your purchase controller, inject $routeParams and access the cost as:
You can use services where you can set the value of cost in one controller and access it in another.
In your product controller, inject dataService and use the setCost Method.
Next, in your PurchaseController, access the value: