Angular路由簡(jiǎn)單學(xué)習(xí)
現(xiàn)在非常流行單頁(yè)面應(yīng)用,傳統(tǒng)都是通過(guò)ajax請(qǐng)求數(shù)據(jù),前端拿到數(shù)據(jù)渲染到頁(yè)面,這種無(wú)刷新的視圖切換非常棒!但是致命的缺點(diǎn)就是刷新後無(wú)法保持原來(lái)的視圖,解決此問(wèn)題的一個(gè)方法是使用 hash,監(jiān)聽(tīng)hashchange事件來(lái)進(jìn)行視圖切換,另一個(gè)方法是用HTML5的history API,通過(guò)pushState()記錄操作歷史,監(jiān)聽(tīng)popstate事件來(lái)進(jìn)行視圖切換,也有人把這叫pjax技術(shù)。
現(xiàn)在開(kāi)始介紹angular的$route!
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$route</title> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-route.min.js"></script> </head> <body> <div ng-controller="Aaa"> <a href="#aaa">首頁(yè)</a> <a href="#bbb">內(nèi)容</a> <a href="#ccc">標(biāo)題</a> <div ng-view></div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',['ngRoute']); m1.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/aaa',{ template : '<h1>AAA</h1>' }).when('/bbb',{ template : '<h1>BBB</h1>' }).when('/ccc',{ template : '<h1>CCC</h1>' }).otherwise({ //默認(rèn)哈希值,哈希值出現(xiàn)錯(cuò)誤也可以執(zhí)行 redirectTo : '/aaa' }); }]); m1.controller('Aaa',['$scope',function($scope){ }]); </script> </body> </html>
上面的例子很簡(jiǎn)單, 除了用template之外還可以用templateUrl引入html的模板文件。
在when傳入控制器的指向,實(shí)現(xiàn)不同的頁(yè)面顯示不同的數(shù)據(jù)。
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$route</title> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-route.min.js"></script> </head> <body> <div ng-controller="Aaa"> <a href="#aaa">首頁(yè)</a> <a href="#bbb">內(nèi)容</a> <a href="#ccc">標(biāo)題</a> <div ng-view></div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',['ngRoute']); m1.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/aaa',{ template : '<h1>AAA</h1>{{name}}', controller : 'Aaa' //控制器指向 }).when('/bbb',{ template : '<h1>BBB</h1>{{name}}', controller : 'Bbb' }).when('/ccc',{ template : '<h1>CCC</h1>{{name}}', controller : 'Ccc' }).otherwise({ redirectTo : '/aaa' }); }]); m1.controller('Aaa',['$scope',function($scope){ $scope.name = 'xiecg-Aaa'; }]); m1.controller('Bbb',['$scope',function($scope){ $scope.name = 'xiecg-Bbb'; }]); m1.controller('Ccc',['$scope',function($scope){ $scope.name = 'xiecg-Ccc'; }]); </script> </body> </html>
以事件的方式映射路由頁(yè)面。
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$route</title> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-route.min.js"></script> </head> <body> <div ng-controller="Aaa"> <a href="javascript:void(0);" ng-click="$location.path('aaa')">首頁(yè)</a> <a href="javascript:void(0);" ng-click="$location.path('bbb')">內(nèi)容</a> <a href="javascript:void(0);" ng-click="$location.path('ccc')">標(biāo)題</a> <div ng-view></div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',['ngRoute']); m1.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/aaa',{ template : '<h1>AAA</h1>{{name}}', controller : 'Aaa' //控制器指向 }).when('/bbb',{ template : '<h1>BBB</h1>{{name}}', controller : 'Bbb' }).when('/ccc',{ template : '<h1>CCC</h1>{{name}}', controller : 'Ccc' }).otherwise({ redirectTo : '/aaa' }); }]); m1.controller('Aaa',['$scope','$location',function($scope,$location){ $scope.name = 'xiecg-Aaa'; $scope.$location = $location; }]); m1.controller('Bbb',['$scope',function($scope){ $scope.name = 'xiecg-Bbb'; }]); m1.controller('Ccc',['$scope',function($scope){ $scope.name = 'xiecg-Ccc'; }]); </script> </body> </html>
項(xiàng)目更復(fù)雜,頁(yè)面相同(首頁(yè)&index),數(shù)據(jù)不同,需要對(duì)url進(jìn)行傳參。
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$route</title> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-route.min.js"></script> </head> <body> <div ng-controller="Aaa"> <a href="javascript:void(0);" ng-click="$location.path('aaa/123')">首頁(yè)</a> <a href="javascript:void(0);" ng-click="$location.path('bbb')">內(nèi)容</a> <a href="javascript:void(0);" ng-click="$location.path('ccc')">標(biāo)題</a> <a href="javascript:void(0);" ng-click="$location.path('aaa/456')">index</a> <div ng-view></div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',['ngRoute']); m1.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/aaa/:num',{ template : '<h1>AAA</h1>{{name}}', controller : 'Aaa' }).when('/bbb',{ template : '<h1>BBB</h1>{{name}}', controller : 'Bbb' }).when('/ccc',{ template : '<h1>CCC</h1>{{name}}', controller : 'Ccc' }).otherwise({ redirectTo : '/aaa/:num' }); }]); m1.controller('Aaa',['$scope','$location','$routeParams',function($scope,$location,$routeParams){ $scope.name = 'xiecg-Aaa'; $scope.$location = $location; console.log($routeParams); //不同的數(shù)據(jù) }]); m1.controller('Bbb',['$scope',function($scope){ $scope.name = 'xiecg-Bbb'; }]); m1.controller('Ccc',['$scope',function($scope){ $scope.name = 'xiecg-Ccc'; }]); </script> </body> </html>
路由的事件監(jiān)聽(tīng)。
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$route</title> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-route.min.js"></script> </head> <body> <div ng-controller="Aaa"> <a href="#aaa">首頁(yè)</a> <a href="#bbb">內(nèi)容</a> <a href="#ccc">標(biāo)題</a> <div ng-view></div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',['ngRoute']); m1.run(['$rootScope',function($rootScope){ //路由切換之前觸發(fā)的事件 $rootScope.$on('$routeChangeStart',function(event,current,pre){ console.log(event); //事件對(duì)象 console.log(current); //路徑對(duì)應(yīng)的數(shù)據(jù)值 console.log(pre); //上一個(gè)路徑 }); }]); m1.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/aaa',{ template : '<h1>AAA</h1>' //templateUrl : 'temp.html' }).when('/bbb',{ template : '<h1>BBB</h1>' }).when('/ccc',{ template : '<h1>CCC</h1>' }).otherwise({ //默認(rèn)哈希值,哈希值出現(xiàn)錯(cuò)誤也可以執(zhí)行 redirectTo : '/aaa' }); }]); m1.controller('Aaa',['$scope',function($scope){ }]); </script> </body> </html>
補(bǔ)充:angular事件的傳播機(jī)制。
<div ng-controller="Aaa"> {{count}} <div ng-controller="Aaa" ng-click="$emit('myEvent')"> {{count}} <div ng-controller="Aaa"> {{count}} </div> </div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',[]); m1.controller('Aaa',['$scope',function($scope){ $scope.count = 0; $scope.$on('myEvent',function(e){ //console.log(e.targetScope); //當(dāng)前的 //console.log(e.currentScope); //目標(biāo)的 //console.log(e.name); //事件名 //e.stopPropagation(); //阻止冒泡 $scope.count++; }); }]); </script>
前面嵌套了三個(gè)controller,我們?cè)谥虚g的controller上綁定了click事件,使用$emit點(diǎn)擊的時(shí)候,上面的controller也會(huì)觸發(fā)事件。
如果是$broadcast點(diǎn)擊就是往下傳播。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angularjs制作簡(jiǎn)單的路由功能demo
- 使用AngularJS對(duì)路由進(jìn)行安全性處理的方法
- angular中使用路由和$location切換視圖
- AngularJS 路由詳解和簡(jiǎn)單實(shí)例
- AngularJS 路由和模板實(shí)例及路由地址簡(jiǎn)化方法(必看)
- angular.js之路由的選擇方法
- AngularJS監(jiān)聽(tīng)路由的變化示例代碼
- 簡(jiǎn)單講解AngularJS的Routing路由的定義與使用
- Angular2 (RC5) 路由與導(dǎo)航詳解
- AngularJs ng-route路由詳解及實(shí)例代碼
相關(guān)文章
Angular2學(xué)習(xí)教程之TemplateRef和ViewContainerRef詳解
這篇文章主要給大家介紹了Angular2中TemplateRef和ViewContainerRef的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05基于angular6.0實(shí)現(xiàn)的一個(gè)組件懶加載功能示例
這篇文章主要介紹了基于angular6.0實(shí)現(xiàn)的一個(gè)組件懶加載功能示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04spring+angular實(shí)現(xiàn)導(dǎo)出excel的實(shí)現(xiàn)代碼
這篇文章主要介紹了spring+angular實(shí)現(xiàn)導(dǎo)出excel的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Angular2關(guān)于@angular/cli默認(rèn)端口號(hào)配置的問(wèn)題
本篇文章主要介紹了Angular2關(guān)于@angular/cli默認(rèn)端口號(hào)配置的問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07AngularJS實(shí)現(xiàn)動(dòng)態(tài)添加Option的方法
這篇文章主要介紹了AngularJS實(shí)現(xiàn)動(dòng)態(tài)添加Option的方法,涉及AngularJS事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05AngularJS基礎(chǔ) ng-mouseenter 指令示例代碼
本文主要介紹AngularJS ng-mouseenter 指令,這里對(duì)ng-mouseenter 指令基礎(chǔ)資料做了詳細(xì)整理,并附代碼實(shí)例,有需要的小伙伴可以參考下2016-08-08angularjs中控制視圖的控制器的兩種注入依賴項(xiàng)及服務(wù)的寫法小結(jié)
在AngularJS中,控制器的依賴注入有兩種方法:顯式依賴注入和隱匿依賴注入,顯式依賴注入通過(guò)使用字符串?dāng)?shù)組形式來(lái)注入依賴項(xiàng),本文給大家介紹angularjs中控制視圖的控制器的兩種注入依賴項(xiàng)及服務(wù)的寫法,感興趣的朋友一起看看吧2024-09-09