2016-12-06 52 views
0

我想創建一個按鈕/標籤與一個onclick事件,做一些功能。不過,我希望能夠給用戶刪除,如果他們want.This按鈕/標籤的選項是我做過什麼:如何用解僱創建按鈕/標籤?

<button type="button" class="btn btn-success" ng- click="doSomething()">Help<span class="glyphicon glyphicon-remove" ng-click="deleteThisButton()"></span></button> 

現在,如果我這樣做,這兩種功能調用。我如何才能根據用戶點擊的位置觸發它。

+0

['event.stopPropogation()'] (https://api.jquery.com/event.stoppropagation/) – philantrovert

回答

0

嘗試......

$('.glyphicon-remove').click(function() { 
    $(this).hide(); 
}); 
+0

雖然此代碼片段可能會解決問題,[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-基於問題的答案)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! – kayess

0

你需要停止click事件傳播到按鈕元素。您可以通過將click事件對象($ event)傳遞給您的deleteThisButton函數來實現此目的。我也包括代碼隱藏按鈕:

$scope.deleteThisButton = function($event) { 
    $event.stopPropagation(); 
    angular.element($event.target).parent().css('visibility','hidden'); 
}; 

你的HTML改成這樣才能在$事件對象傳遞給函數:

<button type="button" class="btn btn-success" ng-click="doSomething()">Help 
     <span class="glyphicon glyphicon-remove" ng-click="deleteThisButton($event)"></span> 
</button> 
相關問題