-1

After using 'use restrict' this directive is not working.

It's not even hitting after using function and 'use strict' its not working

(function () {
    "use strict";
    var appRoot = angular.module("app.top").directive('confirmOnExit', ['$location', 'ConfirmModal', '$timeout', function (location, ConfirmModal, $timeout) {

        return {
            link: function ($scope, element, attrs) {
                $scope.$evalAsync(function () {
                    var unbindChangeSuccess = $scope.$on('$locationChangeStart', function (event, next, current, e) {
                        $scope.DirtyForm = ($scope.componentAddForm.$dirty ? $scope.componentAddForm.$dirty : $scope.resourceForm.$dirty)
                        if ($scope.DirtyForm) {
                            event.preventDefault();
                            alert('Route Changed')                         
                        } else {
                        };
                    });

                })
            }
        };
    }]);
})
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425

1 Answers1

1

It needs to be a self invoking function:

(function () {
    var appRoot = angular.module("app.top").directive('confirmOnExit', ['$location', 'ConfirmModal', '$timeout', function (location, ConfirmModal, $timeout) {

        return {
            link: function ($scope, element, attrs) {
                $scope.$evalAsync(function () {
                    var unbindChangeSuccess = $scope.$on('$locationChangeStart', function (event, next, current, e) {
                        $scope.DirtyForm = ($scope.componentAddForm.$dirty ? $scope.componentAddForm.$dirty : $scope.resourceForm.$dirty)
                        if ($scope.DirtyForm) {
                            event.preventDefault();
                            alert('Route Changed')                         
                        } else {
                        };
                    });

                })
            }
        };
    }]);
})();

For more info check out THIS post.

Kyle Krzeski
  • 5,600
  • 5
  • 36
  • 48