2

我有一個標誌按鈕,當用戶點擊時,標誌着討論,之後標誌按鈕被文本'成功標誌'取代。目前,我在點擊標誌按鈕後無法禁用ng-click。 ng-click對於文本'已成功標記'仍然存在,我想阻止點擊此文本以防止在標記相同討論時發生錯誤。如何在ng-if ng-switch語句內點擊後禁用ng-click?

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

通過添加惰性評估或通過防止傳播,我也許能阻斷do_something()方法被調用,但我我也希望鼠標光標仍然是一個指針,而不是變成「點擊鏈接」鼠標圖標,這可能嗎? 看起來像鼠標按鈕圖像是一個css問題,我修正了

我也試過只是在ng-switch-when語句中添加ng-click語句,如下圖,但點擊後isFlagging仍然是false我沒有得到成功的文本:

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

的[上所有類型的元件的應用的特定條件下禁用納克單擊]可能的複製(http://stackoverflow.com/questions/23194477/disable-ng-click-on-certain-conditions-of-application-for-all-types-of-element) – Luca

+0

以前試過,不行的 – chonglawr

回答

1

您可以停止活動通過對「成功標記」分區添加ng-click="$event.stopPropagation()"冒泡。在這種情況下,點擊事件不會到達父容器:

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

嘿嘿@dfsq,這工作,但有任何方法來防止鼠標圖標變成單擊鏈接圖標時,這樣做? – chonglawr

0

也許是一個簡單的條件?

``$scope.do_something = function() { 
    if ($scope.button_clicked === true) { 
    return; 
    } 
    else { 
    $scope.button_clicked = true; 
    this.isFlagging = true; 
    } 
}`` 
+0

剛剛嘗試過,不起作用:(,ng開關仍然拿起ng點擊 – chonglawr

0

添加在NG-點擊button_clicked是一種常見的圖案

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

+0

剛剛嘗試過,不幸的是我不知道爲什麼點擊按鈕後,它沒有註冊ng開關條件,所以現在甚至沒有看到成功的消息 – chonglawr

+0

嘗試像我在前面的代碼中那樣移動ng開關 –