0
(function(){

    var eateryControllers=angular.module('eateryControllers',[]);

    eateryControllers.controller('MainController',function($scope){
        var mainCtrl=this;
        mainCtrl.$on('$viewContentLoaded',function(){
            console.log("view loaded");
        })
    });

})();

this caused an error: mainCtrl.$on is not a function. Earlier versions did this by injecting $scope. Cant we do this using 'this' keyword and 'Controller as' approach like above way

lch
  • 4,569
  • 13
  • 42
  • 75

1 Answers1

2

In your case, 'this' refers to the controller and not the $scope. For 'this' to refer to $scope, you would need to be within a $scope method. You need to use:

$scope.$on(...)

See also: 'this' vs $scope in AngularJS controllers

Community
  • 1
  • 1