这次给大家带来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可以用来传递参数
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是angular的路由ui-router的使用详解的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号