AngularJS routing
AngularJS routing allows us to access different content through different URLs.
1. First, you need to introduce angular and angular-route on the page, pay attention to introduce angular before angular-route
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="https://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>
This is mainly because angular-route.js needs to pass in the parameter window.angular, and this parameter will only appear after angular is loaded.
2. The routing function is implemented by the routeProvider service and ng-view. ng-view is equivalent to providing a mount point for page templates. When switching URLs to jump, different page templates will be placed where ng-view is located. ; Then configure the route mapping through routeProvider.
Generally, there are two main methods:
when(): configuration path and parameters;
otherwise: Configure other path jumps, which can be thought of as default.
The second parameter of when:
controller: the controller function corresponding to the path, or the name
controllerAs: give the controller an alias
template: the page template corresponding to the path, which will appear at ng-view, such as "<div>xxxx</div>"
templateUrl: The path of the corresponding template, such as "src/xxx.html"
resolve: This parameter emphasizes that this property will bind a service or value to the routing-related controller in the form of a key-value pair object. Then inject the result value of the execution or the corresponding service reference into the controller. If the resolve is a promise object, it will wait for it to execute successfully before injecting it into the controller. At this time, the controller will wait for the execution result in resolve.
For a detailed example, please refer to the following example.
redirectTo: redirect address
reloadOnSearch: Set whether to load the corresponding template only when the address changes; neither search nor params changes will load the template
caseInsensitiveMatch
: Paths are case sensitive
There are several commonly used events for routing:
$routeChangeStart: This event fires before the route jumps
$routeChangeSuccess: This event fires after the route jumps successfully
$routeChangeError: This event fires after a route jump fails
Example:
<div ng-controller="myCtrl">
<ul>
<li><a href="#/a">click a</a></li>
<li><a href="#/b">click b</a></li>
</ul>
<view-div> </div>
</div>
Usually our URLs are of the form http://runoob.com/first/page, but in single page web applications AngularJS is implemented via the # + tag.
<script type="text/javascript">
angular.module("myApp",["ngRoute"])
.controller("aController",function($scope,$route){
$scope.hello = "hello,a!";
})
.controller("bController",function($scope){
$scope.hello = "hello,b!";
})
.controller("myCtrl",function($scope,$location){
$scope.$on("$viewContentLoaded",function(){
console.log("ng-view content loaded!");
});
$scope.$on("$routeChangeStart",function(event,next,current){
//event.preventDefault(); //cancel url change
console.log("route change start!");
});
})
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/a', {
templateUrl: 'a.html',
controller: 'aController'
})
.when('/b', {
templateUrl: 'b.html',
controller: 'bController',
resolve: {
// I will cause a 3 second delay
delay: function($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 3000);
return delay.promise;
}
}
})
.otherwise({
redirectTo: '/a'
});
});
</script>
解析:
1、载入了实现路由的 js 文件:angular-route.js。
2、包含了 ngRoute 模块作为主应用模块的依赖模块。
angular.module('routingDemoApp',['ngRoute'])
3、使用 ngView 指令。
<div ng-view></div>
该 div 内的 HTML 内容会根据路由的变化而变化。
配置 $routeProvider,AngularJS $routeProvider 用来定义路由规则。
AngularJS 模块的 config 函数用于配置路由规则。通过使用 configAPI,我们请求把$routeProvider注入到我们的配置函数并且使用$routeProvider.whenAPI来定义我们的路由规则。
$routeProvider 为我们提供了 when(path,object) & otherwise(object) 函数按顺序定义所有路由,函数包含两个参数:
第一个参数是 URL 或者 URL 正则规则。
第二个参数是路由配置对象。