0

i'm trying to set up the first example linked here: https://www.w3schools.com/angular/angular_routing.asp the example that contains the colors.

<p><a href="#/!">Main</a></p>

<a href="#!red">Red</a>
<a href="#!green">Green</a>
<a href="#!blue">Blue</a>

<div ng-view></div>

<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "main.htm"
    })
    .when("/red", {
        templateUrl : "red.htm"
    })
    .when("/green", {
        templateUrl : "green.htm"
    })
    .when("/blue", {
        templateUrl : "blue.htm"
    });
});
</script>
</body>

If i used template and put HTML in, it works completely fine but when ever I use templateUrl(i do have all of the .htm files set up) it can never find the .htm pages.

My directory is setup as

C:\Users{User}\Documents\ng-Route and all of the .htm files are children of the ng-route folder.

Whenever I click a link, the url goes from: ...ng-Route/index.html#! to ng-Route/index.html#!/green but the partial view doesn't get displayed. Any help would be dearly appreciated.

Jane Doe
  • 21
  • 3
  • 1
    you need to launch your app in a local server. try something simple like [http-server](https://www.npmjs.com/package/http-server) – Aleksey Solovey May 25 '18 at 08:32
  • I think back to the documentation for me. Is this something ng-route requires? a http-server? as i've been able to use AngularJS without one for a while @AlekseySolovey – Jane Doe May 25 '18 at 08:35
  • You don't need a server to run anuglar router, just like you don't need it to run angular. Don't know where the server thing came from.. – Adrian May 25 '18 at 08:36
  • @Adriani6 I was slightly confused by the answer myself, wondering why it was needed. – Jane Doe May 25 '18 at 08:38
  • Are you sure those are `.htm` files not `.html`? If you open Inspector Tools > Network Tab, you can check which path it hits, maybe it goes elsewhere, verify that first. It definitively doesn't require a server.. – Adrian May 25 '18 at 08:42
  • I just checked the console and saw this error: angular.js:12845 Failed to load file:///C:/Users/{username}/Documents/ng-Route/green.htm: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. @Adriani6 i'll give this a google but any help is very much welcomed :) – Jane Doe May 25 '18 at 08:48
  • `ngRoute` provider uses http protocol that's why if you will run this code directly from folder, you will get cross origin issue. Try using `ng-templates` scripts to act like separate `.html` files. E.g. ``. Make sure they are within `ng-app` – Aleksey Solovey May 25 '18 at 08:49
  • @AlekseySolovey You're right about the server. Hope you can forgive me :) I forget I run my stuff via .NET/Cordova that opens a server for me... Apologies. – Adrian May 25 '18 at 08:51
  • Possible duplicate of [Cross origin requests are only supported for HTTP](https://stackoverflow.com/questions/19847252/cross-origin-requests-are-only-supported-for-http) – Aleksey Solovey May 25 '18 at 08:54
  • 1
    @Adriani6 Yeah when i saw the console and saw the cross site stuff the server all made sense. Thank you, quickly just set it up and it's working perfectly now. If you want, create an answer and i'll accept it asap. – Jane Doe May 25 '18 at 08:58

0 Answers0