In AngularJS, the ng-click
directive is used to bind a function to an HTML element, such as a button or a link, so that when the element is clicked, the specified function is executed. Here's how you can use it:
ng-click
directive to the HTML element you want to trigger the function. This can be a button, a link, or any other clickable element.html<button ng-click="myFunction()">Click me</button>
javascriptangular.module('myApp', [])
.controller('myController', function($scope) {
$scope.myFunction = function() {
// Your function logic here
console.log('Button clicked!');
};
});
ng-controller
directive.html<div ng-app="myApp" ng-controller="myController">
<!-- Your HTML content here -->
<button ng-click="myFunction()">Click me</button>
</div>
Now, when the button is clicked, the myFunction()
function will be executed.
Remember to include AngularJS library in your HTML file for AngularJS directives to work:
html<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
That's it! This is how you can use the ng-click
directive in AngularJS to handle click events.