これに似た質問がありますが、異なります。
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 によって認識されていません。
もう1つの質問は$apply()
、スコープを呼び出す必要があると言っていますが、私が利用できるスコープは唯一のものであり、$rootScope
それを呼び出す$apply()
ことは機能していないようです。
回答に対するコメントは、スコープを取得できることを示唆しています
$scope = angular.element(document).scope()
しかし$rootScope
、これはうまくいきません。
の変更を再認識するにはどうすればよい$location.path()
ですか? message
パスを変更できるような方法でコールバックを登録するより良い方法はありますか?