Angular--ui-router的使用

先引用Angular然后引用ui-router

路由清单:我们依赖的ui.router中提供了一个服务$state,此时可以用config来配置这个服务。用$stateProvider的state方法来设置路由清单。也就是说,定义一个个“状态”。

小tip:服务除了factory、service之外,还有一个provider。provider一般工程师不会自己定义的,你只需要知道所有的内置服务都是provider函数写的。provider函数写出的服务,可以在依赖注入前,用config函数配置。

config表示配置,可以配置任何内置服务,此时$state就是插件给我们的服务。一般的,服务对应的Provider就是服务名字后面直接加Provider(下文中黄色背景代码段):

<script type="text/javascript">

var myapp = angular.module("myapp",["ui.router"]);

//配置路由表,实际上在配置$state服务。

myapp.config(function($stateProvider) {

$stateProvider

.state({

name: ‘news‘,

url: ‘/news‘,

template: ‘<h3>新闻频道</h3>‘

})

.state({

name: ‘music‘,

url: ‘/music‘,

template: ‘<h3>音乐频道</h3>‘

})

.state({

name: ‘movie‘,

url: ‘/movie‘,

template: ‘<h3>电影频道</h3>‘

});

});

</script>

配置路由使用$stateProvider的state方法,又是一个装饰者模式。

.state().state().state()装饰了三回,连续打点。

配置的时候有三个配置:名字name、路由url、模板template。

此时页面上不要忘记放置一个

<ui-view></ui-view>

的E级别指令。此时动态的内容(template里面的内容)都将呈现在ui-view里面。

完整代码:

<!DOCTYPE html>

<html lang="en" ng-app="app">

<head>

<meta charset="UTF-8">

<title>路由演示</title>

</head>

<body>

<ui-view></ui-view>

<script type="text/javascript" src="js/angular.min.js"></script>

<script type="text/javascript" src="js/angular-ui-router.js"></script>

<script type="text/javascript">

var app = angular.module("app",["ui.router"]);

//对$state服务进行一个配置,此时可以书写路由清单

app.config(function($stateProvider){

$stateProvider

.state({

name: ‘news‘,

url: ‘/news‘,

template: ‘<h3>新闻频道</h3>‘

})

.state({

name: ‘music‘,

url: ‘/music‘,

template: ‘<h3>音乐频道</h3>‘

})

.state({

name: ‘movie‘,

url: ‘/movie‘,

template: ‘<h3>电影频道</h3>‘

});

});

</script>

</body>

</html>

时间: 2024-07-28 17:11:24

Angular--ui-router的使用的相关文章

angular+ui router+require

index.html !DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.min.css"></h

angular 的ui.router 定义不同的state 对应相同的url

Angular UI Router: Different states with same URL? The landing page of my app has two states: home-public, home-logged-in. Now I want to show both states on the same URL, but let the controller and template depend on the user session (is the user log

angular : $location &amp; $state(UI router)的关系

次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, oldUrl) { log("$locationChangeStart from " + oldUrl) log("$locationChangeStart to " + newUrl) }); 接着是 ui router $state $rootScope.$on(&q

Angularjs中UI Router全攻略

摘自:Angularjs中UI Router全攻略 温馨提示:想要了解 angular-ui-router的同学,从上往下读一遍,能带随着coding那就更好了,保证你对angular-ui-router基本全部掌握. 如何引用依赖angular-ui-router angular.module('app',["ui.router"]) .config(function($stateProvider){ $stateProvider.state(stateName, stateCofi

ngRoute 和 ui.router 的使用方法和区别

在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比. ngRoute 使用方法 1) 引入 angular-route lib 无论是 ngRoute 还是 ui.router ,作为框架额外的附加功能,都必须以 模块依赖 的形式被引入. 1 <script src="lib/angular-route.js"></sc

Angularjs中UI Router用法小记录

今天自己参考已有的项目代码学习了下UI Router的用法,写了个小demo,验证了下自己的想法,现把使用情况记录一下. 1.入口文件index.html,引入项目所需的js文件,标注ng-app,创建ui-view元素,为后面的嵌套做容器准备. <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8">

[转]AngularJS 使用 UI Router 实现表单向导

本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的UI Router以及Angular ngAnimate module创建一个带动画的多步表单.这项技术可以用在你想要简化用户操作的大表单上. 我们看到这项技术已经应用在了许多的网页上.比如购物车,注册表单,入职流程以及许多多步表单,让用户更容易在线填写表单. 下面我们将构建它: 使用UI Rout

[Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?

In this tutorial we are going to learn how we can can configure redirects in the angular 2 router configuration. We are also going to see again another example on how the order of the configuration might trip us. We are going to see the different bet

[Angular 2 Router] Configure Your First Angular 2 Route

Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and then importing the configured RouterModule into your main App Module. Use the Wiki Search as example project. Create a HomeComponent to contain every

angular当router使用userhash:false时路由404问题

angular当router使用userhash:false时路由404问题 安装iis urlrewrite2.0组件 在根目录下创建 Web.config <configuration> <system.webServer> <rewrite> <rules> <rule name="AngularJS Routes" stopProcessing="true"> <match url="