[AgnularJS] Route Life Cycle
If you change a route, basic there are four steps before you can see the final reuslt.
1. on fire, for exmaple:
$location.path("/new");
In this step, there is your only change to do something you need to do before route changed.
2. route change start:
$rootScope.$on("$routeChangeStart", function(){...});
It knows where it should go and where it comes from. But data is not ready yet.
3. route change success:
$rootScope.$on("$routeChangeSuccess",function(){...});
The data is ready, route is changed ono the address panel and tempalte is loaded into the memory, but not yet shown on the screen.
4. show the template on the screen. Get into the new controller.
var app = angular.module("app", ["ngRoute"]); app.config(function ($routeProvider) { $routeProvider .when('/', { templateUrl: "app.html", controller: "ViewCtrl" }).when('/new', { templateUrl: 'new.html', controller: "NewCtrl", resolve:{ loadData: viewCtrl.loadData } }) }); app.directive('error', function($rootScope){ return{ restrict: 'E', template: '<div class="alert-box alert" ng-show="isError">Error!!!</div>', link: function(scope){ $rootScope.$on("$routeChangeError", function (event, current, previous, rejection) { scope.isError = true; }) } } }) app.controller("AppCtrl", function ($scope, $rootScope, $route, $location) { //event on route, depends on root scope $rootScope.$on("$routeChangeStart", function(event, current, previous, resolve){ console.log("-------------2- change start----------------"); //Before any promises been resolved //know where it's going //where it comes from //but not yet data ready console.log($scope, $rootScope, $route, $location); }); $rootScope.$on("$routeChangeSuccess",function(event, current, previous, resolve){ console.log("-------------3- change success----------------"); //Data is ready, //route is change //template has been loaded into memory //but visual not change yet console.log($scope, $rootScope, $route, $location); }); }); var viewCtrl = app.controller("ViewCtrl", function ($scope, $route, $location) { console.log($route); $scope.changeRoute = function(){ console.log("-------------1-before change route----------------"); console.log($scope); //Before the route change, here is the only change to do anything you need to do $location.path("/new"); } }); app.controller("NewCtrl", function($scope, loadData, $template) { console.log("-------------4-all done----------------"); //change the visual console.log($scope, loadData, $template); }); viewCtrl.loadData = function ($q, $timeout) { var defer = $q.defer(); $timeout(function () { defer.resolve({message: "success"}) }, 2000); return defer.promise; }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <title>Egghead Videos</title> <link rel="stylesheet" href="./foundation.min.css"> <script type="text/javascript" src="./angular.min.js"></script> <script type="text/javascript" src="./angular-route.min.js"></script> <script type="text/javascript" src="./app.js"></script> </head> <body> <div ng-app="app" ng-controller="AppCtrl"> <ng-view></ng-view> <error></error> </div> </body> </html>
app.html
<h1><div ng-click="changeRoute()">Change route</div></h1>
new.html
<h1>Hi, I'm new here!</h1>
after cilick, change to
分类:
AngularJS
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具