HappyCoding HappyCoding - 5 months ago 36
AngularJS Question

Write event onclick not via using ng-click in angularjs

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.

Answer

Try angular.element:

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

Same Jquery Code:

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

Demo