我有一個一元這樣如何防止某個元素的click事件是按角度綁定的?
<a class='btn' ng-click="alert('clicked')"></a>
在JS
$scope.alert=(item)=>{alert(item)};
我測試了使用NG-停用,但沒有作用。 這是我的HTML 這是我的指令
我有很多在我的項目一個元素,所以我想寫像NG-禁用怎麼寫指令?
我有一個一元這樣如何防止某個元素的click事件是按角度綁定的?
<a class='btn' ng-click="alert('clicked')"></a>
在JS
$scope.alert=(item)=>{alert(item)};
我測試了使用NG-停用,但沒有作用。 這是我的HTML 這是我的指令
我有很多在我的項目一個元素,所以我想寫像NG-禁用怎麼寫指令?
我認爲這是解決問題的方法,謝謝各位幫我唯一的問題〜
.directive('a', function() {
return {
restrict: "E",
replace: true,
scope: {
},
link: function(scope, elem, attr) {
if (attr['ngDisabled']) {
elem.unbind('click')
}
}
}
})
通過$事件對象的功能,你可以像這樣做,
<a class='btn' ng-click="alert($event,clicked)"></a>
,並在控制器
$scope.alert = function ($event,clicked) {
if (you want to stop) {
$event.stopPropagation();
}
};
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;
唐在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
指令中使用插值。
我想在元素中使用不是按鈕,現在按鈕沒有問題。 –
我有許多像這樣的元素,所以我想解決這個像使用ng-disable,你有什麼好的建議嗎?謝謝.. –
你不需要一個指令,你可以做$ event.stopPropagation(); – Sajeetharan
– Sajeetharan