频道栏目
首页 > 资讯 > 路由器 > 正文

angularjs:路由设置

17-09-07        来源:[db:作者]  
收藏   我要投稿

mycrm项目登录后,进入index界面,设计成左边是一个菜单栏,右边作为一个详情页面,前端采用了angular后使用路由可以很好的解决这个问题;

图1-1

图1-2

具体设计看下面代码:

var aoorey= angular.module('aoorey', [ 'ngRoute','ui.bootstrap','aoorey.controller','aoorey.controller.base','aoorey.controller.blackList',
	'aoorey.controller.guide','aoorey.controller.user','aoorey.controller.order','aoorey.controller.coupons',
	'aoorey.controller.guideAccount','aoorey.controller.guidePunish','aoorey.controller.aooreyHome','aoorey.controller.carData',
	'aoorey.services','aoorey.services.final','aoorey.directives','xeditable' ])
.config([
	'$routeProvider','$httpProvider',
	function($routeProvider, $httpProvider) {
	    $routeProvider.when('', {
			templateUrl : 'test',
			controller : 'dashboard'
	    }).when('/dashboard', {
			templateUrl : 'test',
			controller : 'dashboard'
	    }).when('/menu', {
			templateUrl : 'menu/menu',
			controller : 'menuCtl'
	    }).when('/userRole', {
			templateUrl : 'userRole/role',
			controller : 'userRoleCtl'
	    }).otherwise({

redirectTo : 'dashboard'

});


当登录后,springmvc跳转到index页面,index页面中使用了ng-view
		
		
 

刚登录后不发请求,默认的走路由的.otherwise,进入设置的dashboard页面然后走springmvc的页面跳转

@RequestMapping(value = "/dashboard")

public String dashboard() {

return "dashboard";

}

@RequestMapping(value = "/test")

public String test() {

return "test";

}

图1-3

下面跟上文档

AngularJS 路由允许我们通过不同的 URL 访问不同的内容。

通过 AngularJS 可以实现多视图的单页Web应用(single page web application,SPA)。

通常我们的URL形式为http://runoob.com/first/page,但在单页Web应用中 AngularJS 通过# + 标记实现,例如:

http://runoob.com/#/first
http://runoob.com/#/second
http://runoob.com/#/third

当我们点击以上的任意一个链接时,向服务端请的地址都是一样的 (http://runoob.com/)。 因为 # 号之后的内容在向服务端请求时会被浏览器忽略掉。 所以我们就需要在客户端实现 # 号后面内容的功能实现。 AngularJS 路由 就通过# + 标记帮助我们区分不同的逻辑页面并将不同的页面绑定到对应的控制器上。

实例解析:

  • 1、载入了实现路由的 js 文件:angular-route.js。

  • 2、包含了 ngRoute 模块作为主应用模块的依赖模块。

    angular.module('routingDemoApp',['ngRoute'])
  • 3、使用 ngView 指令。

    
    
     

    该 div 内的 HTML 内容会根据路由的变化而变化。

  • 4、配置 $routeProvider,AngularJS $routeProvider 用来定义路由规则。

    module.config(['$routeProvider', function($routeProvider){
        $routeProvider
            .when('/',{template:'这是首页页面'})
            .when('/computers',{template:'这是电脑分类页面'})
            .when('/printers',{template:'这是打印机页面'})
            .otherwise({redirectTo:'/'});
    }]);

    AngularJS 模块的 config 函数用于配置路由规则。通过使用 configAPI,我们请求把$routeProvider注入到我们的配置函数并且使用$routeProvider.whenAPI来定义我们的路由规则。

    $routeProvider 为我们提供了 when(path,object) & otherwise(object) 函数按顺序定义所有路由,函数包含两个参数:

    • 第一个参数是 URL 或者 URL 正则规则。
第二个参数是路由配置对象。

路由设置对象

AngularJS 路由也可以通过不同的模板来实现。

$routeProvider.when 函数的第一个参数是 URL 或者 URL 正则规则,第二个参数为路由配置对象。

路由配置对象语法规则如下:

$routeProvider.when(url, {
    template: string,
    templateUrl: string,
    controller: string, function 或 array,
    controllerAs: string,
    redirectTo: string, function,
    resolve: object
});

参数说明:

template:

如果我们只需要在 ng-view 中插入简单的 HTML 内容,则使用该参数:

.when('/computers',{template:'这是电脑分类页面'})

templateUrl:

如果我们只需要在 ng-view 中插入 HTML 模板文件,则使用该参数:

$routeProvider.when('/computers', {
    templateUrl: 'views/computers.html',
});

以上代码会从服务端获取 views/computers.html 文件内容插入到 ng-view 中。

controller:

function、string或数组类型,在当前模板上执行的controller函数,生成新的scope。

controllerAs:

string类型,为controller指定别名。

redirectTo:

重定向的地址。

resolve:

指定当前controller所依赖的其他模块。



相关TAG标签
上一篇:Spring学习
下一篇:编写Web页面的注意事项
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站