angularjs – Angular $location.path不工作
发布时间:2020-12-17 09:01:05 所属栏目:安全 来源:网络整理
导读:我有一个类似于 this one的问题,但不同。 这里我试图为一个 window.postMessage 处理程序添加一个事件监听器。 app.run(function ($location,$window,$rootScope) { $window.addEventListener('message',function(e) { $location.path("/abc"); console.log
我有一个类似于
this one的问题,但不同。
这里我试图为一个 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 }); }); Angular不能识别$ location.path。 另一个问题说,我应该在范围上调用$ apply(),但是我可用的唯一范围是$ rootScope,并调用$ apply()似乎不工作。 对答案的评论表明,范围可以得到 $scope = angular.element(document).scope() 但是这给了我$ rootScope,它不工作。 如何获得角度来重新定位$ location.path()中的更改?有没有更好的方式注册一个消息回调的方式,我可以改变路径?
你应该在$ apply()方法中运行表达式as function
app.run(function ($location,function(e) { $rootScope.$apply(function() { $location.path("/abc"); console.log($location.path()); }); }); }); 见documentation – ng.$rootScope.Scope。 如果你想提高可测试性,使用$ console而不是控制台,并注入那个对象。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |