[英]TypeError: Cannot call method '$on' of undefined
Here is my controller.js code in angular app 这是我在angular应用程序中的controller.js代码
function MyCtrl1($scope) {
$scope.$on('$locationChangeStart', function (event, next, current) {
event.preventDefault();
var answer = confirm("Are you sure you want to leave this page?");
if (answer) {
$location.url($location.url(next).hash());
$rootScope.$apply();
}
});
}
MyCtrl1.$inject = [];
function MyCtrl2() {}
MyCtrl2.$inject = [];
When i checked in chrome i got the following error in developer console 当我检查Chrome时,在开发人员控制台中收到以下错误
TypeError: Cannot call method '$on' of undefined
can any one point out what may be wrong. 谁能指出什么是错的。
You need to inject $scope. 您需要注入$ scope。
MyCtrl1.$inject = ['$scope'];
EDIT: The full fix... 编辑:完整修复...
Anything you're passing into your controller, you're going to need to inject, if you're injecting explicitly with ctrl.$inject = [];
如果要使用ctrl.$inject = [];
显式注入,则将要传递到控制器中的所有内容都需要注入ctrl.$inject = [];
function MyCtrl1($scope, $location, $rootScope) {
$scope.$on('$locationChangeStart', function (event, next, current) {
if (!confirm("Are you sure you want to leave this page?")){
event.preventDefault();
}
});
}
MyCtrl1.$inject = ['$scope', '$location', '$rootScope'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.