npm install angular-ui-router
;angular-ui-router.js
;ui.router
。
<html ng-app="myApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js">script>
<script src="js/angular-ui-router.min.js">script>
<script>
var myApp = angular.module('myApp', ['ui.router']);
...
script>
...
head>
<body>
...
body>
html>
ui-view
指令到
中。
<body>
<div ui-view>div>
<a ui-sref="state1">State 1a>
<a ui-sref="state2">State 2a>
body>
ui-sref
用在
标签之中。ui-sref
除了能够实现state的改变,如果某个state有与之对应的URL
,那么ui-sref
还会自动给
标签添加href
属性。接下来我们会定义一些模板,它们会被插入到ui-view
的位置。应该注意的是它们也会有自己的ui-view
。
<h1>State 1h1>
<hr/>
<a ui-sref="state1.list">Show Lista>
<div ui-view>div>
<h1>State 2h1>
<hr/>
<a ui-sref="state2.list">Show Lista>
<div ui-view>div>
ui-view
之中。
<h3>List of State 1 Itemsh3>
<ul>
<li ng-repeat="item in items">{{ item }}li>
ul>
<h3>List of State 2 Thingsh3>
<ul>
<li ng-repeat="thing in things">{{ thing }}li>
ul>
$stateProvider
服务将所有的状态都连接起来。myApp.config(function($stateProvider, $urlRouterProvider) {
//
// For any unmatched url, redirect to /state1
$urlRouterProvider.otherwise("/state1");
//
// Now set up the states
$stateProvider
.state('state1', {
url: "/state1",
templateUrl: "partials/state1.html"
})
.state('state1.list', {
url: "/list",
templateUrl: "partials/state1.list.html",
controller: function($scope) {
$scope.items = ["A", "List", "Of", "Items"];
}
})
.state('state2', {
url: "/state2",
templateUrl: "partials/state2.html"
})
.state('state2.list', {
url: "/list",
templateUrl: "partials/state2.list.html",
controller: function($scope) {
$scope.things = ["A", "Set", "Of", "Things"];
}
});
});
Go to Quick Start Plunker for Nested States & Views
state
有多个view
与之相对应。ui-view
。
<body>
<div ui-view="viewA">div>
<div ui-view="viewB">div>
<a ui-sref="route1">Route 1a>
<a ui-sref="route2">Route 2a>
body>
myApp.config(function($stateProvider) {
$stateProvider
.state('index', {
url: "",
views: {
"viewA": { template: "index.viewA" },
"viewB": { template: "index.viewB" }
}
})
.state('route1', {
url: "/route1",
views: {
"viewA": { template: "route1.viewA" },
"viewB": { template: "route1.viewB" }
}
})
.state('route2', {
url: "/route2",
views: {
"viewA": { template: "route2.viewA" },
"viewB": { template: "route2.viewB" }
}
})
});
Go to Quick Start Plunker for Multiple & Named Views
github上的ui-router文档