【问题标题】:Routing Issue in Angular js not able to RouteAngular js中的路由问题无法路由
【发布时间】:2016-12-07 13:26:46
【问题描述】:

我无法继续在 angularjs 中进行路由,我收到以下错误 "XMLHttpRequest 无法加载 file:///C:/Users/PRn1/Desktop/angular%20samples/home1.html。跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https , chrome-extension-resource。” 我正在使用 Chrome 对其进行测试,如果有任何帮助,将不胜感激

这里是示例代码

这是 app.js

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

app.controller('myCtrl', function ($scope) {  
    $scope.message = "Thank you for visiting our website";  
})  

app.config(function ($routeProvider) {  
    $routeProvider  
        .when('/home', {  
            templateUrl: 'home1.html',  
            controller: 'myCtrl'  
        })  
        .when('/aboutUs', {  
            templateUrl: 'aboutus.html',  
            controller: 'myCtrl'  
        })  
        .when('/contactUs', {  
            templateUrl: 'Contactus.html',  
            controller: 'myCtrl'  
        })  
        .otherwise({  
            redirectTo: '/home'  
        });  
});  

这是 home.html

<html>
<head>
<!--Using CDN for angularjs and angular js routing-->  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>  

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>  

<!--Adding javaScript file for angularJs coding-->  
<script src="app.js"></script>  
</head>
<body>
<div ng-app="myApp">  
<div id="topLinks">  
<a href="#/home1">Home</a>  
<a href="#/aboutUs">About Us</a>  
<a href="#/contactUs">Contact Us</a>  
</div>  
<div ng-view>
</div>   
</div>
</body>

这是 Home1.html

<div>  
    <h1>Welcome to Home page1</h1>  
    {{message}}  
</div> 

这是 Aboutus.html

<div>  
    <h1>Welcome to About us page</h1>  
    {{message}}  
</div>

这是contactus.html

<div>  
    <h1>Welcome to Contact us page</h1>  
    {{message}}  
</div>  

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    从服务器启动它。 Angular 不允许跨域请求。意思是,只需双击文件并希望它会起作用,不会。 安装一个小型服务器。 Live-server 是我使用的。

    【讨论】:

      【解决方案2】:

      您不能像这样在本地加载文件 file:///C:/... 您必须改用 http://。看看http-server,它可以让你快速启动一个服务器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        相关资源
        最近更新 更多