<li ng-repeat="contact in contacts">
<a ui-sref="contacts.detail({ id: contact.id })"</a>
</li>
<a ui-sref='^'>Home</a>
<div ui-view></div>
$stateProvider
.state("home", { ... });
.state("contacts", { ... });
.state("contacts.detail", { ... });
.state("contacts.detail.edit", { ... });
在state中的'.'标明其父子关系。
所以 'contacts.detail'的父页面是 'contacts'.
<div ui-view="named"></div>
$stateProvider.state('simple', { url: '/simple/:param', // or '/simple?param', // or '/simple/{param}', template: viewTemplate, // templateUrl: 'xxx.html', controller: 'viewCtrl' ,
// or 'viewCtrl as view'
// or function($scope,service)
resolve: {} // 作前期数据准备,完成后才会实例化controller,
data: {} // 提供自定义数据
params: {} // 设置默认参数
views: {
'viewname@statename' // 绝对视图概念
} // 精确路由和同一状态多个view同时更新
abstract: boolean (default:false) // 抽象路由
});
$urlRouterProvider.otherwise('/index'); // 路由无法匹配默认跳转地址
$urlRouterProvider.when('A-URL','B-URL'); // 如果URL匹配A重定向到B
url: '/contacts/:contactId'
url: '/contacts/{contactId}'
url: '/contacts/{contactId:[0-9a-fA-F]{1,8}}' //Hexadecimals
url: '/contacts?contactId&contactRegion' //Separate with '&'
//State URL:
url: '/users/:id/details/{type}/{repeat:[0-9]+}?from&to'
//Navigate to:
'/users/123/details//0'
//$stateParams will be
{ id:'123', type:'', repeat:'0' }
//Navigated to:
'/users/123/details/default/0?from=there&to=here'
//$stateParams will be
{ id:'123', type:'default', repeat:'0',from:'there', to:'here' }
.state('parent', {
resolve:{
resA: function(){
return {'value': 'A'};
}
},
controller: function($scope, resA){
$scope.resA = resA.value;
}
})
.state('parent.child', {
resolve:{
resB: function(resA){
return {'value': resA.value + 'B'};
}
},
controller: function($scope, resA, resB){
$scope.resA2 = resA.value;
$scope.resB = resB.value;
}
$stateProvider
.state('parent', { data:{ customData1: "Hello", customData2: "World!" } }) .state('parent.child', { data:{ // customData1 inherited from 'parent' // but we'll overwrite customData2 customData2: "UI-Router!" } }); $rootScope.$on('$stateChangeStart', function(event, toState){ var greeting = toState.data.customData1 + " " + toState.data.customData2; console.log(greeting); })
当 'parent' 被激活,输出 'Hello World!'
当 'parent.child' 被激活,输出 'Hello UI-Router!'
myApp.controller('contactCtrl', ['$scope', '$state',
function($scope, $state){
$scope.goToDetails = function(){
$state.go('contact.details', {id: selectedId});
}
}
Go to parent - $state.go('^')
Go to child - $state.go('.3')
Go to sibling - $state.go('^.1')
Absolute Path - $state.go('3.3')
LOL! - $state.go('^.^.2.1')
Abstract States
(Events)