angularJs中ui.route的簡單應用

 
 
html頁面代碼
<body ng-app="myApp">
  <div ui-view></div>
  <div ui-view="login"></div>
  <div ui-view="enroll"></div>
</body>

須要引用的ui.router.js文件javascript

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

 

app.jshtml

將UI-Router做爲web應用的依賴,注入到主程序:java

url:url選項將會爲該應用的狀態指定一個URL基於用戶瀏覽該應用所在的狀態(地址顯示連接)。這樣當在瀏覽該應用的時候便能實現深度連接的效果。 web

var myApp = angular.module('myApp', ['ui.router']);
myApp.config(['$stateProvider', '$urlRouterProvider', routeConfig]);

function routeConfig($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('');
  $stateProvider.state('competition', {
    url: '/competition',
    templateUrl: '/competition.html',
    controller: 'competitionController'
  }).state('competition.detail', {
    url: '/competition-detail',
    templateUrl:  '/competition-detail.html',
    controller: 'competitionDetailController'
  }).state.('competition.enrollForm',{
    url: '/competition.enrollForm',
    templateUrl:  'competition-enrollFrom.html',
    controller: 'enrollFromController'
  }).state.('competition.comments',{
    url: '/competition-comments',
    templateUrl:  'competition-comments.html',
    controller: 'commentsController'
  }).state('competition.login',{
    url: '/competition-login',
    views: {
      'login@': {
        templateUrl:  'competition-login.html',
        controller: 'loginController'
      }
    }
  }).state('competition.enroll',{
  url: '/competition-enroll',
views: {
    'enroll@': {
     templateUrl: 'competition-enroll.html',
      controller: 'enrollController'
}
}
})
}

須要注意的是:ui.router使用的是$stateProvider,ngRoute使用的是$routeProvider。app

$state.goide

$state.go(to, [,toParams],[,options])
形參to是string類型,必須,使用"^"或"."表示相對路徑;
形參toParams可空,類型是對象;
形參options可空,類型是對象,字段包括:location爲bool類型默認true,inherit爲bool類型默認true, relative爲對象默認$state.$current,notify爲bool類型默認爲true, reload爲bool類型默認爲falseui

$state.go('photos.detail')
$state.go('^')到上一級,好比從photo.detail到photo
$state.go('^.list')到相鄰state,好比從photo.detail到photo.list
$state.go('^.detail.comment')到孫子級state,好比從photo.detail到photo.detial.commenturl

若有錯誤還請及時指出code

相關文章
相關標籤/搜索