Is there a way to deactivate an ng-click function once it has been triggered within an ng-if ng-switch block?

Whenever a user clicks on the flag button, it flags the discussion and then the button changes to display 'successfully flagged'. I am currently facing an issue with disabling the ng-click after clicking the flag button. The ng-click still works for the text 'successfully flagged' and I want to prevent any clicks on this text to avoid errors when flagging the same discussion.

html:

<div ng-if="canFlag(discussion)">
    <div ng-switch="isFlagging"
        ng-disabled="button_clicked"
        ng-click="do_something()"
        id="flag{{discussion.id}}"
        title="{{'flag as inappropriate'}}"
        robo-confirm="{'Are you sure you want to flag this?'}"
        class="feedActionBtn">

        <i ng-switch-when="false"
            class="icon-flag"></i>

        <div ng-switch-when="true" 
            translate translate-comment="success message">
            Successfully flagged</div>
    </div>
</div>

js:

$scope.isFlagging = false;
$scope.button_clicked = false;
    $scope.do_something = function() {
        $scope.button_clicked = true;
        this.isFlagging = true;
    }

By adding a lazy evaluation or by preventing propagation, I might be able to block the do_something() method from being called, but I am also looking to have the mouse cursor remain a pointer and not change to a 'click link' mouse icon, would this be possible? Looks like the mouse button image was a css issue i fixed

I've also tried just adding the ng-click to the ng-switch-when statement, such as the below, but after click, isFlagging is still false and I don't get the success text:

<div ng-switch-when="false"
    ng-click="do_something()"
    id="flag{{discussion.id}}"
    title="{{'flag as inappropriate'}}"
    robo-confirm="{'Are you sure you want to flag this?'}"
    class="feedActionBtn">
    <i class="icon-flag"></i>
</div>

Answer №1

To prevent event bubbling, simply include

ng-click="$event.stopPropagation()"
within the "Successfully flagged" div. By doing so, the click event will be contained within the specified element and not be propagated to the parent container:

<div ng-click="$event.stopPropagation()"
     ng-switch-when="true" 
     translate translate-comment="success message">
    Successfully flagged</div>

Answer №2

Maybe an uncomplicated condition?

``$scope.perform_action = function() {
  if ($scope.clicked_button === true) {
     return;
  }
  else {
  $scope.clicked_button = true;
    this.isFlagging = true;
  }
}``

Answer №3

Utilizing the button_clicked within the ng-click function is a widely adopted practice

<div ng-if="canFlag(discussion)">
  <div ng-click="!button_clicked && do_something()"
    id="flag{{discussion.id}}"
    title="{{'flag as inappropriate'}}"
    robo-confirm="{'Are you sure you want to flag this?'}"
    class="feedActionBtn">
    <div ng-switch="isFlagging">
        <i ng-switch-when="false" class="icon-flag"></i>

        <div ng-switch-when="true" 
        translate translate-comment="success message">
        Successfully flagged</div>
    </div>
  </div>
</div>

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

Avoid having Masonry load all divs simultaneously

I have experience using jQuery Masonry on Tumblr, but now I want to incorporate it into my portfolio website for displaying my photography. However, I'm struggling to find a solution that allows sets of images to load in as the user scrolls to the bot ...

What is the best way to connect objects that have been separated?

Looking for a way to reattach my detached objects and wondering how to replace the function that currently only triggers an alert. Any suggestions or help is appreciated. http://jsfiddle.net/jy2Zj/ In addition, I have another question - is there a method ...

A step-by-step guide on transferring Data URI from canvas to Google Sheet using the fetch method

I am trying to send an image as base64 code to a Google sheet using fetch. However, I am encountering an error that says "data_sent is not defined" when I run my code. I need help identifying the problem and finding a solution to fix it. For reference, & ...

modifying Redux state according to the position in an array

In my food truck app, I have stored customer orders in the redux state as an array of objects. Each order includes a list of items selected by the customer, along with the count, name, and total price for each item. For example: state.order = [ {item: " ...

Organize the array by property name and include a tally for each group

My current data structure looks like this: var data = [ { MainHeader: Header1, SubHeader: 'one'}, { MainHeader: Header1, SubHeader: 'two'}, { MainHeader: Header2, SubHeader: 'three'}, { MainHeader: Header2, SubHea ...

Tips for verifying that input is provided in a text field when the checkbox is marked

Can someone help me with validating a form where the user must enter data in a text field if they check a checkbox? I have JavaScript code for checkbox validation, but need assistance with text field validation. Thank you! $(document).ready(function () ...

Exploring the wonders of Lightbox when dealing with content loaded via AJAX

Using Bootstrap 5 along with the Lightbox library from , I encountered an issue. The Lightbox feature functions properly on regular page loads, but fails to load on content loaded via AJAX. I attempted to resolve this by implementing the following code: ...

Utilizing the Pub/Sub architecture to integrate the kafka-node library within Node Js

Utilizing the kafka-node module in my NodeJs Microservise project, I am aiming to implement a Pub/Sub (publisher and subscriber) design pattern within the Functional programming paradigm. producer.js const client = new kafka.KafkaClient({ kafkaHost: ...

What is the best way to create a unit test for a controller that utilizes $state?

Currently working on creating a unit test for my controller: app.controller('StartGameCtrl', function ($scope, $timeout,$state) { $scope.startGame = function () { $scope.snap = false; $scope.dealCards(); debugger; ...

Encounters a fault while processing the php output

Displaying an error in the query The browser is showing the correct answer. $.ajax({ type: "POST", url: url, async: true, contentType: " charset=utf-8", dataType: "XMLHttpRequest", success: func ...

In Javascript, swap out a particular colored region in an image with a different image

I've been scouring the internet in search of a solution to my problem, but it seems like I might not be looking in the right places. Imagine this image below, how can I replace the blue area with an image uploaded by the user? I'm struggling to ...

What could be the reason for the timeout function in my Angular application not working as expected?

I've created a basic controller to handle a countdown timer. Upon running the code, I notice that only one "tick" is displayed in the console. Ideally, I would like to see a "tick" every 5 milliseconds. .controller('Countdown', function($s ...

Having trouble getting JavaScript to work when returned via AJAX

After triggering a lightbox by clicking on a business name, I encountered an issue when trying to replicate the same functionality using AJAX. The lightbox fails to show up. Can anyone provide assistance? The following code represents a 3rd party publishe ...

Trouble with VueJS refresh functionality

I am facing an issue with a method that needs to run on route load. Despite attempting to call it from the updated hook, it is not functioning as expected. Additionally, I have encountered an ESLint error. methods: { getDeals (key, cb) { this.dealsR ...

What is the process for implementing a version folder for my @types/definition?

Is there a way to access the typings for react-router in my project? My package.json file currently has this dependency: { "@types/react-router": "^4.0.3" } However, it seems to be using the standard index.d.ts file from DefinitelyTyped library which i ...

Modifying the content in one form field based on the information entered in another input field

I have a scheduling application that includes a form for selecting both the departure city and arrival city. The app is designed for international travel only, so when a user selects a city from Hungary as the departure city, I want to exclude all Hungaria ...

Extract data from an API endpoint using JavaScript or React

I have the primary website link, which necessitates an authorization header for access every time. //console.log contains this information - accounts:[{categoryId:"some info"... }] api/v2/accounts To extract accountId and categoryId from the i ...

Guidelines for queuing method calls using Vue.js

Is there a way to invoke a method using a queue system? Imagine having a method that makes API calls and can only handle 3 calls at once. If more than 3 calls are made from a component, the remaining ones should wait until one call finishes before proceedi ...

When an anchor tag is embedded within a string response, ReactJS fails to render it as a clickable link

I am in the process of transitioning our code from Freemarker to React JS and I am encountering an issue with rendering anchor tags from a JSON string response. When I display this string in my React JS application, the anchor tags are appearing as plain t ...

JavaScript Application - Problem with Universal Windows Script

I recently built a website utilizing material design lite for the aesthetics: Here are the scripts included: <script src="./mdl/material.min.js"></script> <script src="Scripts/angular.min.js"></script> The necessary .css fi ...