我有一个与此类似的问题,但有所不同。
在这里,我试图为处理程序添加一个事件侦听器window.postMessage。
app.run(function ($location, $window, $rootScope) {
$window.addEventListener('message', function(e) {
$location.path("/abc");
console.log($location.path()); // this prints "/abc" as expected
$rootScope.$apply(); // this has no effect
$scope = angular.element(document).scope(); // this is the same as $rootScope
$scope.$apply(); // so this also has no effect
});
});
$location.path没有被Angular识别。
另一个问题说我应该调用$apply()范围,但我唯一可用的范围是$rootScope并且调用$apply()它似乎不起作用。
对答案的评论表明可以使用范围
$scope = angular.element(document).scope()
但这给了我$rootScope,这是行不通的。
我如何获得角度来重新识别变化$location.path()?message有没有更好的方法以我可以更改路径的方式注册回调?