26.angularJS $routeProvider

转自: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进行路由跳转。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值