路由ngRoute (需要依赖ngRoute模块)


<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular-route.js"></script>
<div ng-view></div>
<script type="text/ng-template" id="scripttemplae">
scripttemplae value:{{value}}
</script>
<script>
angular.module('app.ui', []).run(["$templateCache",
function($templateCache) {
$templateCache.put("template/index.html",
'<div><input type="text" ng-model="id">\
<a href="/TEST/{{id}}.html">TEST/{{id}}.html</a>\
<a href="/test/{{id}}.xml">test/{{id}}.xml</a>\
<a href="/test/{{id}}.json?a=test">test/{{id}}.json?a=test</a>\
<a href="/test/{{id}}.css">test/{{id}}.css</a>\
</div>');
}]);
angular.module('app.route', ['ngRoute'])
.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider.when('/test/:id.xml', {
templateUrl: 'scripttemplae',
controllerAs:'a',
controller: 'detail',
}).when('/test/:id.html', {
template: 'id:{{value}}',
controller: 'detail',
resolve: {
idfilter: function($q, $route) {
if ($route.current.params.id.length < 2) {
document.write('请输入长度大于2的字符串\
<a href="/a.html">返回</a>');
return $q.reject('sds');
}
$route.current.params.id+="add";
}
},
caseInsensitiveMatch: true,
reloadOnSearch: true
}).when('/test/:id.css', {
template: function(obj) {
return "id:" + obj.id;
},
controller: 'a',
}).when('/test/:id.json', {
redirectTo: function(obj, path, search) {
alert(search.a);
return '/test/' + obj.id + '.html';
}
}).otherwise({
templateUrl: 'template/index.html',
controller: function($scope) {
$scope.id = 2;
}
});
$locationProvider.html5Mode(true);
}]);
angular.module('app', ['app.route', 'app.ui'])
.controller('detail',
function($scope, $route, $routeParams) {
if ($routeParams.id == $route.current.params.id) {
$scope.value = $routeParams.id;
}
});
angular.bootstrap(document, ['app']);
</script>
</html>

上面基本上已经包含设置路由的全部写法,页面上面必须有个ng-view指令,否则失效

  1. $locationProvider.html5Mode(true)是用来开启html5模式因为只有在html5的情况下
    才支持js改url地址,否则只能在后面加上#/test/a却不能改变地址
  2. templateUrl可以是url的地址,或者是$templateCache的key,或者是script标签的id,
    如果是方法返回的也是地址字符串
  3. controller可以是一个控制器名称或者一个方法
  4. controllerAs设置控制器的别名
  5. template可以是是模板字符串,或者一个方法返回模板字符串,方法的参数为$routeParams
  6. redirectTo这个参数是跳转到其他的路由
  7. resolve可以在进入控制器前对参数进行修改或者判断(当调用$q.reject时就不会进入控制器)
  8. caseInsensitiveMatch设置路由的是否大小写不敏感,默认为敏感
  9. reloadOnSearch 判断当前location.hashlocation.search改变是否重新更新路由,即地址栏上面是否有#test或者?a=test的改变,默认是重新更新路由
  10. 在控制器中可以或者$route$routeParams,其中$route.current.params$routeParams一样,$route还包含很多当前路由的信息
  11. otherwise是当没有匹配的路由时候成立

其中路由的地址的匹配有三种/:id,/:id**,/:id?规则?和也就是正则的规范


Ⅲ.AngularJS的点点滴滴-- 路由的更多相关文章

  1. Ⅳ.AngularJS的点点滴滴-- 服务

    服务(Angularjs很多方法都是服务组成的) 1.使用service方法创建的单例服务 <html> <script src="http://ajax.googleap ...

  2. Ⅱ.AngularJS的点点滴滴--缓存

    模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...

  3. Ⅶ.AngularJS的点点滴滴-- 事件

    事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...

  4. Ⅵ.AngularJS的点点滴滴-- 指令

    指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...

  5. AngularJS 的嵌套路由 UI-Router

    AngularJS 的嵌套路由 UI-Router 本篇文章翻译自:https://scotch.io/tutorials/angular-routing-using-ui-router 演示网站请查 ...

  6. AngularJS ui-router (嵌套路由)

    http://www.oschina.net/translate/angularjs-ui-router-nested-routes AngularJS ui-router (嵌套路由) 英文原文:A ...

  7. 关于AngularJs中的路由学习总结

    AngularJs中的路由,应用比较广泛,主要是允许我们通过不同的url访问不同的内容,可实现多视图的单页web应用.下面看看具体怎么使用. 关于路由  通常我们的URL形式为http://jtjds ...

  8. [Angularjs]视图和路由(四)

    写在前面 关于angularjs的路由的概念基本上这篇就要结束了,通过学习,以及在实际项目中的实践,还是比较容易上手的.自己也通过angularjs做了一个在app上的一个模块,效果还是可以的. 系列 ...

  9. [Angularjs]视图和路由(二)

    写在前面 上篇文章主要介绍了视图和路由的基本概念,并在文章最后举了一个简单的使用案例.这篇文章将继续学习路由的配置,及相关参数的说明. 系列文章 [Angularjs]ng-select和ng-opt ...

随机推荐

  1. 【HDU 5456】 Matches Puzzle Game (数位DP)

    Matches Puzzle Game Problem Description As an exciting puzzle game for kids and girlfriends, the Mat ...

  2. [转贴]watin的一些例子

    Some WatiN ExamplesBelow are some examples of WatiN codes for testing:// find div by idvar div = bro ...

  3. 14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构

    14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构 所有的InnoDB indexes 是 B-trees Index r ...

  4. Android中的常见时区

    方法: private void printTimeZone(){ String[] ids= TimeZone.getAvailableIDs(); for (int i = 0; i < i ...

  5. 【CF】283D Tennis Game

    枚举t加二分判断当前t是否可行,同时求出s.注意不能说|a[n]| <= |3-a[n]|就证明无解,开始就是wa在这儿了.可以简单想象成每当a[n]赢的时候,两人都打的难解难分(仅多赢一轮): ...

  6. Translate one

    打开一个新窗口并加载给定URL指定的文档. 导航应用程序窗口到指定的位置. 语法 var retval = window.open(url, name, features, replace); 参数 ...

  7. 如何实现Azure虚拟网络中点到站VPN的自动重连

     在Windows Azure早期版本中,用户要在某台Azure平台之外的机器与Azure平台内部的机器建立专用连接,可以借助Azure Connect这个功能.当前的Azure版本,已经没有Az ...

  8. DataSet用法详细

    转自:http://www.cnblogs.com/zeroone/archive/2012/06/08/2541299.html DataSet用法详细 一.特点介绍 1.处理脱机数据,在多层应用程 ...

  9. include a image in devexpress datagrid

    Add an ImageCollection to yout form and add some icons 16x16 to it. Add a column to the Grid for the ...

  10. javascript在alert()出现中文乱码

    今天在写jquery代码时,alert()总是出现中文乱码 琢磨了好一会儿,在网上查的资料是html页面上设置charset为GB2312 <meta charset="GB2312& ...