AngularJS监听路由变化的方法
网络编程 2021-07-04 19:20www.168986.cn编程入门
本篇文章主要介绍了AngularJS监听路由变化的方法,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
使用AngularJS时,当路由发生改变时,我们需要做某些处理,此时可以监听路由事件,常用的是$routeStartChange, $routeChangeSuess。完整例子如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AngularJS监听路由变化</title> </head> <body ng-app="ngRouteExample"> <div id="navigation"> <a href="#/home" rel="external nofollow" >Home</a> <a href="#/about" rel="external nofollow" >About</a> </div> <div ng-view></div> <script type="text/ng-template" id="home.html"> <h1> Home </h1> <table> <tbody> <tr ng-repeat="x in records" style="background:#abcdef;"> <td>{{x.Name}}</td> <td>{{x.Country}}</td> </tr> </tbody> </table> </script> <script type="text/ng-template" id="about.html"> <h1> About </h1> <p>在输入框中尝试输入:</p> <p>姓名:<input type="text" ng-model="name"></p> <p>你输入的为: {{name}}</p> </script> <script src="http://apps.bdimg./libs/angular.js/1.4.6/angular.min.js"></script> <script src="http://apps.bdimg./libs/angular-route/1.3.13/angular-route.js"></script> <script type="text/javascript"> angular.module('ngRouteExample', ['ngRoute']) .config(function ($routeProvider) { $routeProvider. when('/home', { templateUrl: 'home.html', controller: 'HomeController' }). when('/about', { templateUrl: 'about.html', controller: 'AboutController' }). otherwise({ redirectTo: '/home' }); }) .run(['$rootScope', '$location', function($rootScope, $location) { /* 监听路由的状态变化 */ $rootScope.$on('$routeChangeStart', function(evt, next, current){ console.log('route begin change'); }); $rootScope.$on('$routeChangeSuess', function(evt, current, previous) { console.log('route have already changed :'+$location.path()); }); }]) .controller('HomeController', function ($scope) { $scope.records = [{ "Name" : "Alfreds Futterkiste", "Country" : "Germany" },{ "Name" : "Berglunds snabbköp", "Country" : "Sweden" },{ "Name" : "Centro ercial Moctezuma", "Country" : "Mexico" },{ "Name" : "Ernst Handel", "Country" : "Austria" }] }) .controller('AboutController', function ($scope) { $scope.name = '呵呵'; }); </script> </body> </html>
上述的例子是AngularJS 1的,对于Angular2是否也可以用,还没尝试过,有机会验证了再记录下咯~~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程