2016-08-25 70 views
1

我正在使用ui路由器,但它不工作。這裏是我的代碼:使用ui路由器在AngularJS中路由

在我的index.html

<li> <a ui-sref="day2">Day 2</a> </li> 
    <li><a ui-sref="day3">Day 3</a></li> 
    <li><a ui-sref="day4">Day 4</a></li> 

我main.js

var myModule = angular.module('myApp', ['ngMessages', 'ui.router']); 
myModule.config(function($stateProvider, $urlRouterProvider) { 
     $urlRouterProvider.otherwise("/day1"); 

     $stateProvider 

     .state('day1', { 
      url: '/day1', 
      templateUrl: 'index.html' 
     }) 
     .state('day1.example', { 
      url: '/theview', 
      template: '<h3>I am a routed view</h3>' 
     }) 

     .state('day2', { 
     url: '/day2', 
     views: { 

      // the main template will be placed here (relatively named) 
      '': { 
      templateUrl: 'day2.html' 
      } 
     } 
     }) 

     .state('day3', { 
     url: '/day3', 
     views: { 

      // the main template will be placed here (relatively named) 
      '': { 
      templateUrl: 'day3.html' 
      }, 

      // the child views will be defined here (absolutely named) 
      '[email protected]': { 
      templateUrl: 'directives.html' 
      }, 

      // for column two, we'll define a separate controller 
      '[email protected]': { 
      templateUrl: 'service.html' 
      } 
     } 

     }) 

     .state('day4', { 
     url: '/day4', 
     views: { 
      '': { 
      templateUrl: 'day3.html' 
      } 
     } 

     }); 
    }); 

當我在瀏覽器上的鏈接2天,第3天和第4天點擊它不工作。即使day1.example不工作但我把它稱爲像這樣在我的index.html

<div> 
    <a ui-sref=".example" class="save button">Example</a> 
</div> 
<div ui-view> </div> 
</div> 

我不知道是什麼問題。我已經下載了ui-router縮小文件,所以這不是問題。它實際上是在瀏覽器中檢測第1天的路由file:///Users/Projects/AngularJs/index.html#/day1 我正在關注scotch教程。

我的問題是與templateUrl: 'day2.html當我將其更改爲template: '<h1> Check if it Works </h1>第2天的鏈接工作正常。

+1

不要設置狀態模板使用'index.html'。 – Phil

回答

1

不要給index.html頁面添加任何狀態,如果你想添加任何內容然後添加子狀態或使用子頁面。
試試這個: -

var myModule = angular.module('myApp', ['ngMessages', 'ui.router']); 
myModule.config(function($stateProvider, $urlRouterProvider) { 
     $urlRouterProvider.otherwise("/day1"); 

    $stateProvider 

     .state('main', { 
      url: '', 
      abstract: true, 
      templateUrl: 'main.html' 
     }) 

     .state('main.day1', { 
      url: '/day1', 
      templateUrl: 'main.html' 
     }) 
     .state('main.day1.example', { 
      url: '/theview', 
      template: '<h3>I am a routed view</h3>' 
     }) 

     .state('main.day2', { 
      url: '/day2', 
      templateUrl: 'day2.html' 
     }) 

     .state('main.day3', { 
      url: '/day3', 
      templateUrl: 'day3.html' 
     }) 

     .state('main.day4', { 
      url: '/day4', 
      templateUrl: 'day3.html'   
     }); 
    }); 
+1

直接打開文件會導致'templateUrl'出現問題。當我加載'file:/// Users/Projects/AngularJs/index.html'時,它將URL檢測爲'file:/// Users/Projects/AngularJs/index.html#/ day1',所以當我點擊鏈接第2天它被翻譯爲不存在的'file:/// day2'。 我的解決方案是使用服務器,我通常使用nginx,所以我跑我的虛擬機,啓動我的nginx服務器,現在當我加載'localhost:8080'時,它會在我的主頁上運行網址'localhost:8080 /#/ day1' (第1天的代碼)和第2天的鏈接'本地主機:8080 /#/ day2'與代碼爲第2天(day2.html),一切工作正常。 –