0

I want to set event onclick for an object not via using ng-click like jQuery syntax:

<button>Click me</button>
<script>
    $('button').click(function () {
        alert('Hello World!')
    });
</script>

My question, is there a way to write event onclick like that in angularjs?

I don't want to use this way:

<button ng-click="sayhello()">Click me</button>

angular.module('myModule', []).controller('myController', function ($scope) {
    $scope.sayhello = function () {
        alert('Hello World!');
    };
});

I want to hide the information ng-click="sayhello()" in the html.

1
  • In my opinion, this would defeat the purpose of using angular in the first place. Directives, such as ng-click are necessary for angular to make sense. Of course, if you are not against using directives, but just want to avoid ng-click for some reason (even though I must admit I can't understand why this would be the case), you can always write your own directive. Commented Jun 20, 2016 at 16:52

2 Answers 2

1

Try angular.element:

var ele = angular.element('button');

Same Jquery Code:

ele.click(function () {
   alert('Hello World!')
});

Demo

Sign up to request clarification or add additional context in comments.

Comments

0
<button click-me>Click me</button>

angular.module('myModule')
  .directive('clickMe', function(){
    return {
      link: function(scope, element){
        element.on('click', function(){
          alert('Wassup');
        })
      }
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.