我有這一塊的HTMLAngular.js,取消NG單擊事件
<button ui:confirm ng:click="action"></button>
的和JavaScript代碼
的.directive('uiConfirm', function()
{
return {
restrict: 'A',
link: function(scope, element, attrs)
{
element.bind('click.confirm', function(event)
{
event.preventDefault();
event.stopPropagation();
});
}
}
})
現在,我想要做的,就是取消ng:click事件,從指令中。 但ng:無論我做什麼,點擊仍然會被觸發。
演示:Fiddle
編輯: 順便提及,在此範圍內產生誤差:
element.bind('click.confirm', function(event)
{
causeAnError();
event.preventDefault();
event.stopPropagation();
});
是否招,和canceles事件傳播,而且還引發和難看的錯誤= )
編輯2: 最後我找到了解決方案!
.directive('uiConfirm', function()
{
return {
restrict: 'A',
link: function(scope, element, attrs)
{
element.bind('click', function(event)
{
scope.$eval(attrs.uiConfirm); // this line of code does the magic!
});
}
}
})
編輯3:
最終解決
.directive('uiConfirm', function()
{
return {
restrict: 'A',
link: function(scope, element, attrs)
{
/**
* Clicking the trigger start the confirmation process.
*/
element.bind('click.confirm', function(event)
{
// not confirmed?
if(! element.data().confirmed)
{
element.data().confirmed = true;
element.addClass('btn-danger');
}
// is already confirmed..
else
{
element.trigger('mouseout.confirm');
scope.$eval(attrs.uiConfirm);
}
});
/**
* Leaving the element, resets the whole process.
*/
element.bind('mouseout.confirm', function()
{
// reset all values
element.data().confirmed = false;
element.removeClass('btn-danger');
});
// reset the whole process on the first run
element.trigger('mouseout.confirm');
}
}
})
點擊一個按鈕,在第一時間,要讓它紅色,並且不會觸發任何動作。單擊第二次,調用該操作。離開按鈕重置整個過程。
@Arun P約翰尼 - 謝謝你小提琴! – 2013-03-13 13:19:47
您正在嘗試執行的操作可能與此https://groups.google.com/forum/#!msg/angular/ADylKzNT5oI/n6ZagosZZgsJ – 2013-03-13 13:24:30
@ArunPJohny相關我已經看到了這一點,但這是另一種場景。 – 2013-03-13 13:27:37