简体   繁体   中英

AngularJS TypeError: Cannot call method 'apply' of undefined

I get this error in AngularJS:

TypeError: Cannot call method 'apply' of undefined
    at Object.$get.e.$emit (http://localhost/TestPortal/js/lib/angular.min.js:87:296)
    at j (http://localhost/TestPortal/js/lib/angular.min.js:146:351)
    at http://localhost/TestPortal/js/lib/angular.min.js:88:100
    at Array.forEach (native)
    at m (http://localhost/TestPortal/js/lib/angular.min.js:6:193)
    at Object.$get.e.$broadcast (http://localhost/TestPortal/js/lib/angular.min.js:88:63)
    at http://localhost/TestPortal/js/lib/angular.min.js:80:114
    at k.promise.then.i (http://localhost/TestPortal/js/lib/angular.min.js:76:119)
    at k.promise.then.i (http://localhost/TestPortal/js/lib/angular.min.js:76:119)
    at http://localhost/TestPortal/js/lib/angular.min.js:76:352 

When calling this line of code in my controller:

function ClientController($scope, $routeParams, $location, StaticData, Client, IndustryFactory) {
     $scope.$on('$viewContentLoaded', someMethod);
}

Tried below which also doesn't help, same error:

ClientController.$inject = ['$scope', '$routeParams', '$location', 'StaticData', 'Client', 'IndustryFactory'];

You need to include $scope explicitly in your controller like this to make it work.

function ClientController($scope, $routeParams, $location, StaticData, Client, IndustryFactory) {
     $scope.$on('$viewContentLoaded', someMethod);
}


ClientController.$inject = ['$scope', '$routeParams', '$location'];

Figured it out. See revised code:

$scope.$on('$viewContentLoaded', function() { someMethod() });

This i believe is issue with minification of your code. Either inject the dependencies as described by @jayaram or use the the [] notation for dependencies and using modules. See the section Notes on Minification here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM