咱们有不少方法让一个视图随着用户的操做进行变化。
可是,只是单单一个视图就要知足全部的需求会让代码变得很是复杂。
也许咱们可使用ng-include
来引用各类模板,但这只限于部分场景。html
因而咱们能够将视图拆分为两种:html5
如此一来,咱们即可以使用route
实现模板和布局视图的组装,以构建多视图的应用。windows
ngRoutes
并不属于核心模块,咱们须要额外引用angular-route.js
,并在声明应用时:浏览器
var myApp = angular.module('myApp',['ngRoute']);
route须要经过$routeProvider
定义,好比这样:ide
var myApp = angular.module('myApp', ['ngRoute']) .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/', { template: '<h2>contacts</h2>', controller: 'myController' }) .when('/contacts', { templateUrl: 'contacts.html', controller: 'contactsController' }) .when('/contacts/:userId', { templateUrl: 'contact.html', controller: 'contactController' }) .otherwise({redirectTo: '/'}); }]);
OMG,这种定义方式太晦涩了,咱们不能像定义指令时用directive()
那样用route()
什么的吗?
其实directive()
什么的都是config()
的语法糖。函数
好比咱们这样声明一个指令:布局
var myApp = angular.module('myApp', []) .directive('myDirective', function() { return { template: '<p>Kavlez</p>' }; });
实际上是:url
var myApp = angular.module('myApp', []) .config(function($compileProvider){ $compileProvider.directive('myDirective', function() { return { template: '<p>Kavlez</p>' }; }); });
provider
用来实例化依赖并对外提供API,好比这里的$route
服务就是经过相应的$routeProvider
对外提供API,好比when()
,咱们经过这些API设置路由规则。code
另外,provider
是怎么来的?
Injector
对module进行配置时,会在这些module中注册全部定义的provider,必要时会注入服务,而服务的实例化则须要provider来进行。htm
不一样的路由模式下URL会以不一样的格式呈现,咱们有两种模式能够选择。
标签模式
AngularJS默认使用这个模式,这个模式下URL会以'#'开头。
html5模式
这个模式下的URL和通常的没什么区别,若是选择了该模式,AngularJS会根据浏览器重写全部的<a href=""></a>
。
路由模式也经过$routeProvider
进行设置,好比:
var myApp = angular.module('myApp', ['ngRoute']) .config(['$locationProvider', function($locationProvider) { $locationProvider.html5Mode(false); $locationProvider.hashPrefix('!'); }])
这里使用的when(path,route)
一共两个参数。
$location.path
匹配,后面带上:
则表示参数,能够传递给$routeParams
。ng-view
的元素里。template
同样,只是经过XHR得到模板。
咱们用ng-view
接收对应的视图模板,给$route
对应的视图占位。
mg-view
的优先级为1000,也就是说AngularJS不会运行同一个元素上的低优先级指令。
ngView指令遵循如下规则。
既然涉及到了路径,就不得不说$location
服务。
感受和windows.location
差很少,但$location
只能用来在内部访问路由,没法刷新整个页面。
下面列出$location
的经常使用的一些方法。
path()
当前路径:
$location.path();
跳转至:
$location.path('/');
replace()
这个方法可让用户没法后退,好比这样:
$location.path('/').replace();
search() :
用于设置URL中的查询参数,好比:
$location.search({id:'000000',name:'Kavlez'}); $location.search('id=000000&name=Kavlez');
有几个路由相关的事件以下:
$routeChangeStart : 路由变化以前会触发该事件,有三个参数,分别是AngularJS事件对象、将要路由的url、当前url。
$rootScope.$on('$routeChangeStart', function(evt, next, current) { //something });
reloadOnSearch
为false,从新使用控制器的实力时触发。