2016-05-13 38 views
0

我正在用angularJS在頁面之間導航,但是當我打開主頁時與我的瀏覽器,它不顯示任何內容 當我在控制檯看,我看到這個錯誤:AngularJS錯誤只有協議方案支持跨源請求:http,data,chrome,chrome-extension,https,chrome-extension-resource

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. 

這裏是我的代碼:

ViewsTestPage:

<html> 
<head> 
    <title>Views Test</title> 
    <script src="angular.min.js"></script> 
    <script src="angular-route.min.js"></script> 
    <script src="controller.js"></script> 
</head> 
<body ng-app="myApp"> 
    <div ng-view></div> 
</body> 
</html> 

的controller.js:

var mainApp = angular.module('myApp', ['ngRoute']); 

mainApp.config(function($routeProvider){ 
    $routeProvider 
    .when('/', { 
     templateUrl: 'UsersPage.html' 

    }) 

    .when('/helloUser', { 
    templateUrl: 'hello.html' 
}) 
.otherwise({ 
    redirectTo: '/' 
}); 

}); 

UsersPage:

Hello world <a href="#/helloUser">Hi users</a> 

hello.html的

<h1> Users Page</h1> 

任何幫助,請我'begginner !!!!

回答

0

發生此錯誤是因爲您只是直接從瀏覽器打開html文檔。要解決這個問題,你需要從web服務器提供你的代碼並在localhost上訪問它。如果您有Apache安裝程序,請使用它來爲您的文件提供服務。有些IDE內置了Web服務器,如JetBrains IDE,Eclipse .. 如果您有webstorm,那麼您可以從webstorm打開它。

如果您有Node.Js設置,那麼您可以使用http-server。只要運行npm install http-server -g,你就可以在終端上使用它,比如http-server C:\location\to\app.

+0

我看到了這個迴應,但我不知道該怎麼做,你能展示更多,我有什麼步驟做??? –

+0

您是否安裝了節點或安裝了webstorm? – sumair

相關問題