使用angular-ui-router替代ng-router

angular框架自身提供的ng-route在一定程度上满足了我们的需求,但是他只针对于单视图,比如点击一个link跳转到另一个视图,但是在实际业务中,需要一个视图中还包含其他的视图,这时ng-route就不满足了,所以我们需要使用第三方的路由插件ui-router。

1. 引入依赖

<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdn.bootcss.com/angular.js/1.3.7/angular.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-router/1.0.3/angular-ui-router.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-bootstrap/2.5.0/ui-bootstrap.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js"></script>
<script src="js/angular-ui-router-me.js"></script>

2. html页面

<body ng-app="routerApp">

 	<div class="list-group">
 		<a class="list-group-item" ui-sref="parent1">111</a>
 		<a class="list-group-item" ui-sref="parent2">222</a>
 		<a class="list-group-item" href="#">333</a>
 		<a class="list-group-item" href="#">444</a>
 		<a class="list-group-item" href="#">555</a>
 	</div>

 	<div ui-view></div>

 	<script type="text/ng-template" id="parent1">
 		<div>this is parent1 page{{test}}</div>
 		<div ui-view="child1" style="border:1px solid red; width: 400px; height: 300px;"></div>
 		<div ui-view="child2" style="border:1px solid blue; width: 300px; height: 200px;"></div>
 	</script>

 		<script type="text/ng-template" id="parent1-c1">
 		<div>this is parent1 child1 page{{test}}</div>
 	</script>

 		<script type="text/ng-template" id="parent1-c2">
 		<div>this is parent1 child2 page{{test}}</div>
 	</script>

</body>

 3. 配置路由的js

var uirouterModule = (function(app){

	var myrouter = angular.module("routerApp", ["ui.router", "ui.bootstrap"]);

	myrouter.config([‘$stateProvider‘, ‘$urlRouterProvider‘, function ($stateProvider, $urlRouterProvider) {

		$stateProvider
		.state(‘parent1‘, { //单一视图
			url: ‘/p1‘, //显示在地址栏中的url
			templateUrl: ‘parent1‘,
			controller: ‘p1‘
		})
		.state(‘parent1‘, { // 一个状态多个视图
			url: ‘/p1‘,
			views: {
				‘‘: {
					templateUrl: ‘parent1‘,
					controller: ‘p1‘
				},
				‘[email protected]‘: { //视图名称@状态名称
					templateUrl: ‘parent1-c1‘,
					controller: ‘p1c1‘
				},
				‘[email protected]‘: {
					templateUrl: ‘parent1-c2‘,
					controller: ‘p1c2‘
				}
			}
		})

		.state(‘parent2‘, {
			url: ‘/p2‘,
			templateUrl: ‘parent2‘,
			controller: ‘p2‘
		});
		$urlRouterProvider.otherwise(‘/p1‘);
	}]);

		myrouter.controller(‘p1‘, function($scope) {
		$scope.test = ‘p1‘;
	});

	myrouter.controller(‘p1c1‘, function($scope) {
		$scope.test = ‘p1c1‘;
	});

	myrouter.controller(‘p1c2‘, function($scope) {
		$scope.test = ‘p1c2‘;
	});

	app.myrouter = myrouter;

	return app;

}(uirouterModule || {}));

  

时间: 2024-08-06 10:54:12

使用angular-ui-router替代ng-router的相关文章

发布ABP v0.19包含Angular UI选项

发布ABP v0.19包含Angular UI选项 ABP v0.19已发布,包含解决的~90个问题和600+次提交. 新功能 Angular UI 终于,ABP有了一个SPA UI选项,使用最新的Angular框架.Angular的集成不是简单地创建了一个启动模板. 创建了一个基础架构来处理ABP的模块化,主题和其他一些功能.此基础结构已部署为NPM包. 为帐户,身份和租户管理等模块创建了Angular UI包. 创建了一个最小的启动模板,使用IdentityServer进行身份验证并使用AS

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

[Angular2 Router] Lazy Load Angular 2 Modules with the Router

Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start up faster because it only needs to use the main App Module when the page initially loads. As you navigate between routes, it will load the additional

[Angular 2] Router basic and Router Params

When we define router in Angualr 2, we use @RouteConcfig() When we want to display component, we use <router-outlet> When we want to navigate to component, we use [routerLink]="['routerName']" When we want to access router params, we use R

angular ui $modal 使用 option

$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示html标签 scope:一个作用域为模态的内容使用(事实上,$modal会创建一个当前作用域的子作用域)默认为$rootScope controller:为$modal指定的控制器,初始化$scope,该控制器可用$modalInstance注入 resolve:定义一个成员并将他传递给$modal指

Angular UI框架 Ng-alain @delon的脚手架的生成开发模板

前言 首先感谢下 cipchk基于 Ng-Zorror 框架上制作的ng-alain . 之前很早就关注了 ng-alain,今天得空折腾了下. 折腾的时候发现官方文档有些坑,没有写清楚,所以我作为一些补充吧~ 之前在微软MVP群里,董斌辉邀请了cipchk做了一次分享,厚着脸皮要了PPT,看了看ng-alin的定位.跟我的项目契合度 很高.那么我们就试试水呗. 正文 说事情,要PPT别找我. 如何使用 安装&配置 第一种方式: 直接 clone git 仓库 $ git clone --dep

轻量级router[类似laravel router]

github地址:https://github.com/NoahBuscher/Macaw/blob/master/Macaw.php 代码加上一些注释,方便以后再看. <?php namespace NoahBuscher\Macaw; /** * method static Macaw get(string $route, Callable $callback) * method static Macaw post(string $route, Callable $callback) * m

[mobile angular ui]MAUI中的font awesome图标

MAUI中用font awesome替换了glyphicon,但是FA中都有哪些可用的图标呢,在网上搜了一张font awesome的对照表,使用时记着把其中的icon-xxx替换为fa-xxx就可以了. 此外,在MAUI中,还支持7个级别大小的图标显示,只添加".fa"是基本大小,另外的六种图标尺寸需要添加的类分别为:".fa-lg",".fa-2x",".fa-3x",".fa-4x",".f

Angular UI Route

实际的项目中,尽量使用ui-router代替ngRoute 前提条件: 下载angular-ui-router.min.js,导入到项目. 优势:因为ngRoute不支持嵌套的ng-view,而ui-router支持,能够构建比较复杂的页面布局. 基于state来进行导航 (state = url + controller + templateUrl) [和 ngRoute的区别是什么呢?] 注意事项: 1)html页面中,用ui-view替换ng-view 2)html页面中的 a 标签用 [

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