欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

angular的路由ui-router的使用详解

程序员文章站 2024-01-03 23:01:10
...
这次给大家带来angular的路由ui-router的使用详解,使用angular路由ui-router的注意事项有哪些,下面就是实战案例,一起来看一下。

UI-router

安装:npm install --save angular-ui-router

配置路由状态

angular.module("myApp").config(function($stateProvider,$urlRouterProvider){
           $stateProvider
           .state({
                name:'main',
                url:'./',
                template('<div>this is a main</div>')
                     })
             .state({
                      name:'home',
                      url:'/home',
                      template:'<p>this is  home</p>'
              })
            .state({
                     name:'about',
                     url:'/about',
                     template:'<h3>Welcome hello</h3>'
                 })            //设置默认跳转
           $urlRouterProvider.otherwise('/')
           }
      )

多模块、多路由、多控制器 处理方式

引入模块

   <script src="./angularjs/angular.js"></script>
    <script src="./js/ctrl1.js"></script>
    <script src="./js/ctrl2.js"></script>
    <script src="node_modules/angular-ui-router/release/angular-ui-router.js"></script>

模块依赖

var app = angular.module('myApp', ['ui.router', 'myApp.ctrl1', 'myApp.ctrl2']);

路由配置

app.config(function($stateProvider, $urlRouterProvider) {
        $stateProvider.state({
                name: 'main',
                url: '/my',
                templateUrl: './test.html',
                controller: 'ctrl1'
            })             /*
            1.设置一个为空匹配 url:'/my'
            2. 在增加一个 路由名字前半部份相同但是后面不同的名字
            * */
            .state({
                name:'my.page',
                url:'/:page'
            })
            .state({
                name: 'home',
                url: '/home',
                templateUrl: './angularjs/app.html',
                controller: 'ctrl2'
            })
            .state({
                name: 'about',
                url: '/about',
                template: '<div>about</div>',
                controller: 'ctrl3'
            })
        $urlRouterProvider.otherwise('/')
    })

$stateParams 获取参数。

在控制器里面注入。能获取地址栏后面跟的参数。

-<ui-view ui-sref=' '></ui-view>

ui-sref可以用来传递参数

相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!

推荐阅读:

JavaScript的定时器详解

JavaScript运行机制之事件和回调函数

浏览器的多线程机制详解

单线程JS与多线程浏览器的使用

以上就是angular的路由ui-router的使用详解的详细内容,更多请关注其它相关文章!

上一篇:

下一篇: