2014-10-02 20 views
0

我嘗試學習AngularJS。目前我專注於ui-Routing。我的代碼非常簡單,但它可以工作,我不知道爲什麼。我的問題基本的UI路由示例,但發生錯誤

說明:

http://de.tinypic.com?ref=kd22ow.jpg

的瀏覽器顯示: 的 「Hello World UI路由!」 (index.html中的h1)

如果我嘗試輸入index.html/home。 它說:找不到錯誤文件!

這裏是我的代碼:

的index.html

<html> 
    <head> 
     <title>My Angular App!</title> 
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script> 
     <script src="app.js"></script> 
    </head> 
    <body ng-app="Test" ng-controller="MainCtrl"> 
    <h1>Hello World ui-Routing!</h1> 
     <div ui-view> 
     </div> 
    </body> 
</html> 

app.js:

angular.module('Test', ['ui-router']) 

.config([ 
'$stateProvider', 
'$urlRouterProvider', 
function($stateProvider, $urlRouterprovider) 
{ 
    $stateProvider 

    .state('home', { 
     url: '/home', 
     templateUrl: '/home.html', 
     controller: 'MainCtrl' 
    }); 

    .state('test', { 
     url: '/test', 
     templateUrl: '/test.html', 
     controller: 'TestCtrl' 
    }); 

    $urlRouterProvider.otherwise('home'); 
}]) 

.controller('MainCtrl', ['$scope', 
function($scope){ 
$scope.test = "Hello Jan!"; 
}); 

.controller('TestCtrl', ['$scope', 
function($scope){ 
$scope.test = "Hello Redirect!"; 
}); 

的test.html:

<h2>Hello Test! </h2> 

home.html的:

<h2>Hello Home! </h2> 

我的問題在哪裏?

期待您的回答, 揚

+0

如何管理靜態文件? – Unrealsolver 2014-10-02 12:15:07

+0

所有文件都在同一個文件夾中。 – Peter 2014-10-02 18:37:20

+0

也許,您必須安裝服務器。例如:npm install static-server; static-server – Unrealsolver 2014-10-03 14:05:45

回答

1

你需要在你的HTML頁面的UI路由器腳本太它是一個第三方,而不是包含在角。

+0

我已添加此表達式。但它也可以工作。同樣的錯誤。 每個文件(index.html,home.html,test.html和app.js)都位於同一個文件夾中。這是一張圖片Image and video hosting by TinyPic Peter 2014-10-02 18:30:37

+0

你是從網絡服務器上運行文件還是直接從硬盤運行?如果以後,這是你的問題,你將需要一個網絡服務器,因爲鉻從光盤加載文件時有限制。您可以按照本指南跳過這些限制http://www.chrome-allow-file-access-from-file.com/ – 2014-10-03 05:17:50

+0

感謝您的答案是解決方案。 – Peter 2014-10-31 14:23:08