AngularJS onclick call function

AngularJS onclick call function

If you are trying to call function on button click using AngularJs please use below code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>AngularJS on click function call</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>

	var app = angular.module('myApp', []);
	
	app.controller('controller', ['$scope', function($scope) {
	    $scope.function1 = function(msg) {
	         alert(msg + ' first function call!');   
	    };
	    $scope.function2 = function(msg) {
	         alert(msg + ' second function call!');   
	    };
	}]);
</script>
</head>
<body>
	<div data-ng-app="myApp">
		<div data-ng-controller="controller">
			<button data-ng-click="function1('AngularJS')">Call first function</button>
			<button data-ng-click="function2('AngularJS')">Call second function</button>
		</div>
	</div>
</body>
</html>

 

Output:

AngularJS onclick call function

 

4 thoughts on “AngularJS onclick call function”

Leave a Reply

Your email address will not be published. Required fields are marked *