2

I have an a element like this

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

in js

$scope.alert=(item)=>{alert(item)};

I had tested use ng-disabled but dose not work. this is my html this is my html this is my directive this is my directive

I have many a element in my project so I want to write a directive like ng-disable how to write it?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Potato Running
  • 408
  • 4
  • 11

4 Answers4

1

pass the $event object to the function, and you can do it like this,

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

and in Controller

$scope.alert = function ($event,clicked) {
  if (you want to stop) {
    $event.stopPropagation();
  } 
};
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

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>

when the value of click is true then the function will be called else will not get called

Please click on both anchor tags to find the difference

in your controller,

$scope.click = false;

Here is the fiddle

Sravan
  • 18,467
  • 3
  • 30
  • 54
0

Don't use interpolation in the ng-disabled directive.

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

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

Using interpolation (double curly brackets) converts the boolean to a string. The string "false" is truthy. This is a quirk of JavaScript. Don't use interpolation in the ng-disabled directive.

THE DEMO on PLNKR

georgeawg
  • 48,608
  • 13
  • 72
  • 95
0

I think this is the way to solve the problem,thank you for all of you to help me solve the problem~

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

        },
        link: function(scope, elem, attr) {
            if (attr['ngDisabled']) {
                elem.unbind('click');
            }
        }
    }
});
Marty
  • 302
  • 4
  • 18
Potato Running
  • 408
  • 4
  • 11