在$ $ rootScope。$ on`中访问$ rootScope和$ scope

时间:2016-04-14 15:25:03

标签: angularjs

我需要在URL更改时设置$ rootScope.VARIABLE。

我怎么能在$ rootScope。$回调中做到这一点?

$rootScope.$on('$locationChangeSuccess', function(event, absNewUrl, absOldUrl) {
    $rootScope.alert =True; // $rootScope is undefined
});

1 个答案:

答案 0 :(得分:0)

将代码用于 myApp.run 上下文。



myApp.run(["$rootScope", "$window", "$location",function ($rootScope, $window, $location) {
	$rootScope.$on('$locationChangeSuccess', function(evt, absNewUrl, absOldUrl) {
		$rootScope.actualLocation = $location.path();
	});
	$rootScope.$on('$locationChangeStart',function(evt, absNewUrl, absOldUrl) {
		$rootScope.actualLocation = $location.path();
	});
}]);