Angular $rootScope.$on Undefined

2019-02-22 00:46发布

问题:

I have the following in a controller.

$rootScope.$on('progressbarEvent', function (event, data) {
    $rootScope.progresscurrent = data.currentprogress;
    console.log('Event listening: ' + $rootScope.progresscurrent);
});

I have this in a factory service.
   $rootScope.$emit('progressbarEvent', dataFactory.currentprogress);

$on is returned undefined. Anyone knows the cause of this?

My controller is:

app.controller('indexcontroller', ['$rootScope','$scope', '$http', 'dataFactory',
       function ($scope,$http,$rootScope, dataFactory) {

回答1:

The order of dependencies has to be consistent with the array it was declared in:

app.controller('indexcontroller', ['$rootScope','$scope', '$http', 'dataFactory',
   function ($rootScope, $scope, $http, dataFactory) {