2016-10-22 36 views

回答

0

我認爲這是解決問題的方法,謝謝各位幫我唯一的問題〜

.directive('a', function() { 
return { 
    restrict: "E", 
    replace: true, 
    scope: { 

    }, 
    link: function(scope, elem, attr) { 
     if (attr['ngDisabled']) { 
      elem.unbind('click') 
     } 
    } 
} 

})

1

通過$事件對象的功能,你可以像這樣做,

<a class='btn' ng-click="alert($event,clicked)"></a> 

,並在控制器

$scope.alert = function ($event,clicked) { 
    if (you want to stop) { 
    $event.stopPropagation(); 
    } 
}; 
+0

我有許多像這樣的元素,所以我想解決這個像使用ng-disable,你有什麼好的建議嗎?謝謝.. –

+0

你不需要一個指令,你可以做$ event.stopPropagation(); – Sajeetharan

+0
-1

var myApp = angular.module('myApp',[]); 
 

 
function MyCtrl($scope) { 
 
    $scope.alert=(item)=>{alert(item)} 
 
    
 
    $scope.click = false 
 
    $scope.click_two = true 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script> 
 
    <body ng-app="myApp"> 
 
    <div ng-controller="MyCtrl"> 
 
    <a style=" color: red" ng-click="!click||alert('Test')">Click on this and this will not alert</a> <br> <br> 
 
    <a style=" color: blue" ng-click="!click_two||alert('Test')">Click on this and this Will alert</a>  
 
</div> 
 
    </body>

當點擊的價值是tru E然後該函數將被其他人稱爲不會得到所謂的

請點擊這兩個錨標籤,找到差異

在你的控制器

$scope.click = false; 

Here is the fiddle

+0

downvoter,請運行該片段和檢查小提琴,工作正常 – Sravan

+0

@Potato運行,請運行我的片段。你發現它有什麼不對嗎? – Sravan

+0

@Potato跑步,我的代碼片段有什麼問題嗎? – Sravan

0

唐在ng-disabled指令中不使用插值。

<!--ERRONEOUS --> 
<button ng-click="clicks=clicks+1" ng-disabled="{{checked}}">Button</button> 

<!--CORRECT --> 
<button ng-click="clicks=clicks+1" ng-disabled="checked">Button</button> 

使用插值(雙括號括號)將布爾值轉換爲字符串。字符串「false」是truthy。這是一個JavaScript的怪癖。 請勿在ng-disabled指令中使用插值。

DEMO on PLNKR

+0

我想在元素中使用不是按鈕,現在按鈕沒有問題。 –

相關問題