-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
  • 732,580
  • 175
  • 1,330
  • 1,459

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
  • 6,183
  • 6
  • 41
  • 52