在angularjs路由加载后运行javascript代码 [英] Run javascript code after angularjs route loaded
问题描述
我需要在angularjs加载路由后显示警报.显示警报的代码在由angularjs异步加载的视图中:
I need to show an alert after a route has been loaded by angularjs. The piece of code to show the alert is in the view loaded asynchronously by angularjs:
<script>
alert('test');
</script>
一旦视图已加载,我希望它可以运行,但不会运行.我知道我可以广播并告诉它以后再运行,但是我需要一个更通用的解决方案.
Once the view has loaded, I expect this to be run, but it does not. I know I can broadcast and tell it to run afterwards etc, but I need a more generic solution.
推荐答案
假设您正在谈论基于角度SPA内部的ngRoute
的路线更改
Assuming you are talking about route changes based upon ngRoute
internal to an angular SPA
将$ route注入您的控制器:
Inject $route into your controller:
.controller('MyCtrl', function($scope, $route){});
并在您的控制器中订阅路由更改事件:
and in your controller you subscribe to the event of the route changing:
$scope.$on('$routeChangeSuccess', function() {
alert('test'); // <-- alert from the question
});
这篇关于在angularjs路由加载后运行javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!