这是我在angular app中的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 = [];
当我签入chrome时,我在开发者控制台中收到以下错误
TypeError: Cannot call method '$on' of undefined
任何人都可以指出可能出错的地方。
答案 0 :(得分:1)
你需要注入$ scope。
MyCtrl1.$inject = ['$scope'];
编辑:完整修复...
如果您明确注射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'];