How can I disable an ng-click after a click inside a ng-if ng-switch statement?

442 Views Asked by At

I have a flag button that when a user clicks on, flags the discussion, and afterwards the flag button is replaced with text 'successfully flagged'. Currently I am having trouble from disabling ng-click after clicking the flag button. ng-click still exists for the text 'successfully flagged' and i want to block clicks on this text to prevent errors on 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>
3

There are 3 best solutions below

1
dfsq On

You can stop event bubbling by adding ng-click="$event.stopPropagation()" on "Successfully flagged" div. In this case click event will not reach parent container:

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

Perhaps a simple conditional?

``$scope.do_something = function() {
  if ($scope.button_clicked === true) {
     return;
  }
  else {
  $scope.button_clicked = true;
    this.isFlagging = true;
  }
}``
2
Emiliano Barboza On

Adding the button_clicked in the ng-click is a common pattern

<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>