0

i have a problem with AngularJS, and it is that i wrote a function inside my controller, and i want to call it inside an event listner, but i get undefinded !!

The Code controller looks like:

inputApp.controller('my_controller', ['$scope', '$upload', '$http', '$filter', '$sce', '$timeout', 
    function ($scope, $upload, $http, $filter, $sce, $timeout) {
                $scope.changeBlock = function (block) {
                    // DO SOMETHING
                };
                $scope.$on('$locationChangeStart', function($scope, next, current){
                       // Calling the following function makes an error
                       $scope.changeBlock('Block_NAME');
                       $scope.preventDefault();                
                });
}]);

so calling that function inside $scope.$on..... make an error:

ERROR:$scope.changeBlock is not a function !!

Could you please help me, how can i call my function inside that listner ?!

Regards Wael

2 Answers 2

1

you mixed up the signature of the event handler and were overriding your $scope variable mistakingly. The first parameter of the event handler is "event" not "$scope":

this should work:

inputApp.controller('my_controller', ['$scope', '$upload', '$http', '$filter', '$sce', '$timeout', 
    function ($scope, $upload, $http, $filter, $sce, $timeout) {
            $scope.changeBlock = function (block) {
                // DO SOMETHING
            };

         // before:
         // $scope.$on('$locationChangeStart', function($scope, next, current){
         // now:
            $scope.$on('$locationChangeStart', function(event, next, current){
                   // Calling the following function makes an error
                   $scope.changeBlock('Block_NAME');
                   $scope.preventDefault();                
            });
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, i did not notice that, i just copy and pasted the code from somewhere on the net
0

Change first parameter name : $scope from $scope.$on('$locationChangeStart', function($scope, next, current){ to something else

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.