1

I've been writing a simple angular directive to show alert when user closes the browser.

angular.module('myApp').directive("areYouSure", ["$window", function ($window) {
    return {
        restrict: "E",
        link: function() {
            alert("I am called");
            $window.onbeforeunload = function () {
                alert("I am called");
                return "are you sure?";
            };
        }
    };
}]);

<div ng-app="myApp">
<areYouSure></areYouSure>

Link function is not being called. what am I missing here

1
  • Are you linking the file to the page? How are your files structured? Commented Jul 20, 2018 at 2:55

1 Answer 1

3

You should call the directive like this,

<are-you-sure></are-you-sure>

DEMO

var app = angular.module('myApp',[])
 
app.directive("areYouSure", ["$window", function ($window) {
    return {
        restrict: "E",
        link: function() {
            alert("I am called");
            $window.onbeforeunload = function () {
                alert("I am called");
                return "are you sure?";
            };
        }
    };
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<are-you-sure></are-you-sure>
</body> 

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

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.