Part 39 AngularJS route change events

In this video we will discuss
1. Different events that are triggered when a route change occurs in an angular application
2. Logging the events and event handler parameters to inspect their respective properties

When route navigation occurs in an Angular application, the following events are triggered
1. $locationChangeStart
2. $routeChangeStart
3. $locationChangeSuccess
4. $routeChangeSuccess

The following code proves the above point

.controller("studentsController", function ($http, $route, $rootScope, $log) {

var vm = this;

$rootScope.$on("$locationChangeStart", function () {

$log.debug("$locationChangeStart fired");

});

$rootScope.$on("$routeChangeStart", function () {

$log.debug("$routeChangeStart fired");

});

$rootScope.$on("$locationChangeSuccess", function () {

$log.debug("$locationChangeSuccess fired");

});

$rootScope.$on("$routeChangeSuccess", function () {

$log.debug("$routeChangeSuccess fired");

});

vm.reloadData = function () {

$route.reload();

}

$http.get("StudentService.asmx/GetAllStudents")

.then(function (response) {

vm.students = response.data;

})

})

Please note that we are injecting $log service into the controller function to log the events.

In our previous video, we used $$route.originalPath property to get the route that the user is navigating to. How do we know next parameter has $$route.originalPath property. Well the easiest way is to log and inspect their properties. The following code does exactly the same thing.

.controller("studentsController", function ($http, $route, $scope, $log) {

var vm = this;

$scope.$on("$routeChangeStart", function (event, next, current) {

$log.debug("RouteChangeStart Event");

$log.debug(event);

$log.debug(next);

$log.debug(current);

});

$scope.$on("$locationChangeStart", function (event, next, current) {

$log.debug("LocationChangeStart Event");

$log.debug(event);

$log.debug(next);

$log.debug(current);

});

vm.reloadData = function () {

$route.reload();

}

$http.get("StudentService.asmx/GetAllStudents")

.then(function (response) {

vm.students = response.data;

})

})

In this example we have logged just $routeChangeStart & $locationChangeStart events parameters. In a similar way, you can also log $routeChangeSuccess & $locationChangeSuccess events parameters.

Now, launch the browser developer tools and you can see the properties available

时间: 2024-12-26 06:55:30

Part 39 AngularJS route change events的相关文章

[AngularJS] Accessible Button Events

Often buttons need to be handled by JavaScript, and if done improperly it can lead to accessibility issues. In this lesson you will improve a major news organization's global header with some basic HTML and JavaScript. Normal you should use native 'b

[Redux] Fetching Data on Route Change

We will learn how to fire up an async request when the route changes. A mock server data: /** /api/index.js * Created by wanzhen on 7.6.2016. */ import { v4 } from 'node-uuid'; // This is a fake in-memory implementation of something // that would be

Cisco IOS debug command reference

Command A through D debug aaa accounting through debug auto-config debug aaa accounting : to display information on accountable events as they occur(in privileged EXEC mode) no debug aaa accounting : to disable debugging output debug aaa authenticati

AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)

AngularCSS CSS on-demand for AngularJS Optimize the presentation layer of your single-page apps by dynamically injecting stylesheets as needed. AngularCSS listens for route (or states) change events, adds the CSS defined on the current route and remo

AngularJs ng-route路由详解

本篇基于ng-route来讲下路由的使用...其实主要是 $routeProvider 搭配 ng-view 实现. ng-view的实现原理,基本就是根据路由的切换,动态编译html模板. 更多内容参考:Angularjs总结 前提 首先需要在页面引入angular和angular-route,注意要在angular-route之前引入angular <script src="../../bower_components/angular/angular.js"></

[AngularJS] ngModelController render function

ModelValue and ViewValue: $viewValue: Actual string value in the view. $modelValue: The value in the model that the control is bound to. In Anuglar, it watchs the $modelValue for you and update $viewValue. As you need to tell Angular when you set $vi

angularJS 服务三

路由 一 简介 1.路由机制 为了实现无刷新的视图切换,我们通常会用ajax请求从后台取数据,然后套上HTML模板渲染在页面上,然而ajax的一个致命缺点就是导致浏览器后退按钮失效,尽管我们可以在页面上放一个大大的返回按钮,让用户点击返回来导航,但总是无法避免用户习惯性的点后退.解决此问题的一个方法是使用 hash,监听hashchange事件来进行视图切换,另一个方法是用HTML5的history API,通过pushState()记录操作历史,监听popstate事件来进行视图切换,也有人把

如何用angularjs制作一个完整的表格之五__完整的案例

由于本人也是边学边写,因此整理的比较乱,下面放出我例子的完整代码,方便大家交流测试,如有问题欢迎评论 首先,表格采用的是BootStrap样式编辑的,主要使用的是angularjs,为了方便也有jQuery的方法,在测试时需自行引入bootstrap,angularjs和jq的文件. 整体代码预览: HTML: <!DOCTYPE html> <html lang="en" ng-app="myModule"> <head> //

如何用angularjs制作一个完整的表格之四__自定义ng-model标签的属性使其支持input之外的html元素

有的时候我们需要为非input类型的元素添加ng-model来实现双向的数据绑定,从而减少冗余代码,那么可以尝试一下的方式 例如:我页面中使用了contenteditable这个属性来实现用户可直接编译的div元素 html: <style> .text{ margin:0 auto; width:100px; height:50px; border:1px solid red; } </style> </head> <body> <div ng-co