angular中可以使用插件,例如ngRoute插件就是用与路由控制。

首先要在模块中引入即可:

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

然后我们进行供应商配置

m1.config(['$routeProvider',function($routeProvider){
    
    $routeProvider
        .when('/aaa/:num',{
            template : '<p>首页的内容</p>{{name}}',
            controller : 'Aaa'
        })
        .when('/bbb',{
            template : '<p>学员的内容</p>{{name}}',
            controller : 'Bbb'
        })
        .when('/ccc',{
            template : '<p>课程的内容</p>{{name}}',
            controller : 'Ccc'
        })
        .otherwise({
            redirectTo : '/aaa'
        });
    
}]);

如上,通过when方法来制定不同的hash值对应不同的视图,hash只后面还可以带上参数num,

如何改变hash值?

1.可以通过a链接的href属性

2.通过$location.path()方法来改变

m1.controller('Aaa',['$scope','$location','$routeParams',function($scope,$location,$routeParams){
    
    $scope.name = 'hello';
    $scope.$location = $location;
    
    console.log( $routeParams );
    
}]);

 

还可以在run方法中监听路由事件。

m1.run(['$rootScope',function($rootScope){
    
    $rootScope.$on('$routeChangeStart',function(event,current,pre){
        console.log(event);
        console.log(current);
        console.log(pre);
    });
    
}]);

 

posted on 2015-11-23 15:08  toodeep  阅读(311)  评论(0编辑  收藏  举报