Modify the data in the local storage and showcase it on the webpage without the need to refresh the page

Currently, I am working on a project using Angular 8. One of the issues I am facing is with a variable called 'cartproductitems'. I want this variable to update automatically whenever a user adds a new product to the cart, so that the number of items in the cart gets updated. Everything works fine except for the fact that I have to manually refresh the page every time a new product is added to the cart.

HTML Code Snippet

<div class="fw-700 mainNavCol" *ngIf="brand_Name!='Pipasha Restaurant'">
        <div class="fa-4x" style="position: relative !important;">
            <button class="btn-cart" (click)="callCheckout()"><i class="fa fa-shopping-cart"></i>
              <span class="hidden-phone">My Trolley</span>
            </button>
            <span class="fa-layers-counter" style="background:Tomato; font-size: xx-large;">{{(""+cartProductItems)?.length}}</span>
        </div>
      </div>

TypeScript Code Snippet

cartProductItems: number;
this.cartProductItems = this._requestService.getProductCartItems();
    console.log('cartProductItems -> '+this.cartProductItems);
  callCheckout() {
    let isTable: boolean = JSON.parse(this._local.get('isTable'));
    if(isTable){
      this._requestService.tableCheckout(this.cartProductItems);
    }
    else{
      this._requestService.checkout(this.cartProductItems);
    }
    console.log('callCheckout -> isTable -> '+isTable);
  }

Answer №1

Have you experimented with specifying the button-Type for your button element? In cases where no type is specified, certain browsers default to using them as submit buttons, potentially triggering unintended server requests.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to display JQuery mobile tap event information in real-time?

I am experiencing an issue in my code where the tap event is being triggered, but the array[i] value is printed as null. Whenever I click on any index, it always prints an empty string (" "). Why does it display null instead of the clicked value? I am see ...

Issues with Read More/Less functionality on a website - Troubleshooting with JavaScript, jQuery, and Bootstrap 4

I'm looking to create a simple, lightweight feature where clicking on a "read more" link will reveal a paragraph, and then clicking on a "read less" link will hide it again. My knowledge of JS and JQuery is quite limited. I'm currently utilizing ...

How to dismiss a jQueryMobile dialog without triggering a page refresh

I've encountered a question similar to this before, but there wasn't any solution provided. The issue I'm facing is that I have a form and when a user clicks on a checkbox, I want to open a popup/dialog for them to enter some data. However, ...

Enhance your React Native app: Utilizing dynamic string variables with react-native-google-places-autocomplete query

I have encountered an issue while attempting to pass a dynamic country code to restrict search results. Here is the code in question: let loc = 'de' <GooglePlacesAutocomplete placeholder="Search" autoFocus={true} onPress ...

Unable to access MongoDB API via standard HTTP requests

I've been attempting to access the MongoDB API using an API key for authentication, but I keep receiving a 401 status code. Below is the code snippet I've been using: fetch(‘https://eu-west-2.aws.data.mongodb-api.com/app/application-0-dhdvm/en ...

jQuery .click() only triggering upon page load

I've been searching all over the place and I just can't seem to find a solution to my specific situation. But here's what I'm dealing with: Instead of using inline HTML onclick, I'm trying to use jQuery click() like this $(docume ...

Designating a certain function to a designated button

On my page, I have three different forms with submit buttons in each. There is a code that is supposed to change the value of a button in a specific form when clicked. However, whenever I click on any submit button, all the values in the buttons of the v ...

Pattern for Ajax callback in Javascript

I'm facing an issue with the code snippet below. var bar = { ajaxcall : function(){ var _obj = {}; $.ajax({ headers: { 'Content-Type': "application/json; charset=utf-8", 'da ...

One possible rewrite: "Iterate through an array of coordinates to determine the distance

I've developed a running app that keeps track of the runner's location and saves the coordinates into an array successfully. Now, I'm facing an issue where I need to loop over the array to calculate the total distance traveled. Here is the ...

Struggling with implementing basic i18n and dynamic routing functionality in Next.js

Despite reading the Next.js documentation, completing the tutorial, and reviewing numerous examples, I am still struggling to accomplish a straightforward task with Next.js. In my application, I have two locales: en and de. The route structure is as foll ...

How can we achieve a programmatic single selection in PrimeNG Tree v16 with checkbox selection option?

Currently, I am working on implementing the PrimeNG Tree component in Angular with selectionMode set to "checkbox". My goal is to achieve a programmatic single selection where selecting a child node automatically unselects all other child and parent nodes ...

Is it possible to iterate over a non-reactive array in a Vue template without having to store it in a data property or computed property?

Presented below is a static array data: const colors = ["Red", "Blue", "Green"]; To display these values in my markup, I can use the following method in React: import React from "react"; // Static array of colors const colors = ["Red", "Blue", "Green"] ...

Failure to update the DOM after clicking a button and making an AJAX request

After experimenting with different iterations of this code, the outcomes have been mediocre and inconsistent. The concept is rather simple: A user inputs a search query into a text box, clicks a button, and the query is assigned to a var search. This varia ...

Ionic4: Troubleshooting the playback of audio files on a web-based application

Currently, my project involves using Ionic 4 in combination with Angular. I am facing a challenge where I need to play an audio file (mp3/wav) when an image is clicked. Despite working on this for a week, I have been unable to figure it out. Can anyone pr ...

What could be causing the visibility issue with my navigation items in the bootstrap navigation bar?

Currently, I am experiencing a puzzling issue with the navigation bar on my live website. The navigation items seem to flicker for a brief moment and then disappear entirely. This unexpected behavior is certainly causing me some concern. I crafted the us ...

Caution: When using Array.prototype.reduce(), make sure the arrow function returns a value

const filteredParams = [...appliedFilters].reduce((prev, curr) => { if (curr.key !== "multi") return { ...prev, ...curr.selectedValue.params }; return; }, {}); When I run this code, I encounter the following warning message in ...

Can we set a restriction on the maximum number of children a particular div can contain?

I'm trying to find a way to restrict the number of children in a div. The concept is that there is a selected commands div where you can drag and drop available commands (essentially buttons) from another div called available commands. However, I wan ...

Selenium is detecting a textbox as hidden, despite it being visible in the browser on my end

My password textbox code is as follows: <input class="blahblah" id="someId" type="password"></input> While I can manually interact with this textbox in the browser, when attempting to automate testing using selenium, an error is thrown sayin ...

How can we ensure a generic async function with a return type that is also generic in Typescript?

I'm currently working on a function that retries an async function multiple times before rejecting. I want to make sure the typescript typings of the retry function are maintained and also ensure that the passed function is of type PromiseLike. Creat ...

Leveraging Express Mergeparams in TypeScript

I've run into an issue while working on my small project in Typescript. The problem arises when I attempt to nest my router, as Typescript doesn't seem to acknowledge the parent's parameter. Within the "child" file, I have the following cod ...