博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
26.angularJS $routeProvider
阅读量:7029 次
发布时间:2019-06-28

本文共 1871 字,大约阅读时间需要 6 分钟。

转自:https://www.cnblogs.com/best/tag/Angular/

O'Reilly书上的伪代码

var someModule = angular.module('someModule',[...module dependencies]);someModule.config(function($routeProvider){    $routeProvider        .when('url',{controller:aController, templateUrl:'/path/to/template'})        .when(...)//other mappings for your app        .otherwise(...)//what to do if nothing else matches});

 

$route被用于URLS到controller和view(HTML模板)之间的链接,它会监控$location.url()并试图对此路径及对应的路由配置进行映射,使用时需要注入安装ngroute模块。

var someModule = angular.module('someModule',['ngRoute']);

举个详细的栗子

var app = angular.module('app', ['ngRoute']);    app.config(function ($routeProvider){        $routeProvider            .when('/other', {                controller: 'otherCtrl',                templateUrl: 'content/views/other.html',                publicAccess: true            })            .when('/', {                controller: 'homeCtrl',                templateUrl: 'content/views/home.html'            })            .when('/other/:id', {                controller: 'otherDetailCtrl',                templateUrl: 'content/views/otherDetail.html',                publicAccess: true            })            .otherwise({                redirectTo: '/'            });    }app.controller('homeCtrl',function($scope,$http){    console.log('i am home page');    $scope.title = 'i am home page';});app.controller('otherCtrl',function($scope){    console.log('i am other page');    $scope.title = 'i am other page';});app.controller('otherDetailCtrl',function($scope, $routeParams, $location){    var id = $routeParams.id;    if(id == 0) {        $location.path('/other');    }    console.log('i am otherDetailCtrl page');    $scope.title = 'i am otherDetailCtrl page';});

在$route(路由)中,提供了两个依赖性服务:$location、$routeParams。

$location、$routeParams均可在controller中使用,如上otherDetailCtrl中,可通过$routeParams获取路由时所传参数,可通过$location进行路由跳转。

你可能感兴趣的文章
封装了些文件相关的操作
查看>>
什么是Solr
查看>>
poj2386(简单dfs)
查看>>
双链表的基本操作
查看>>
走进异步编程的世界 - 剖析异步方法(上)
查看>>
[HAOI2006]受欢迎的牛
查看>>
docker-maven-plugin 完全免Dockerfile 文件
查看>>
day20 Python 装饰器
查看>>
限制性与非限制性定语从句区别
查看>>
fiddler工具的使用
查看>>
jquery源码分析(二)——架构设计
查看>>
javascript深入理解js闭包(转)
查看>>
207. Course Schedule
查看>>
如何优化您的 Android 应用 (Go 版)
查看>>
Trie树实现
查看>>
Opencv无法调用cvCaptureFromCAM无法打开电脑自带摄像头
查看>>
Exception异常处理机制
查看>>
复杂的web---web中B/S网络架构
查看>>
编写文档的时候各种问题
查看>>
Eclipse里maven的project报Unbound classpath variable: 'M2_REPO/**/***/***.jar
查看>>