A function like: ng-click="alert('Clicked')"

user2227400 picture user2227400 · Jan 20, 2016 · Viewed 19.6k times · Source

For a mockup I need a simple mechanism like

ng-click="alert('Clicked')"

but the code above is not working, can someone help me? I don't want to touch the Controller..

Answer

Neeraj Bansal picture Neeraj Bansal · Feb 7, 2018

Refer to previous answer, ng-click = "alert('Hello World!')" will work only if $scope points to window.alert i.e

$scope.alert = window.alert;

But even it creates eval problem so correct syntax must be:

HTML

<div ng-click = "alert('Hello World!')">Click me</div>

Controller

$scope.alert = function(arg){
    alert(arg);
}