Angularjs routing not working
Asked Answered
R

2

14

Everything worked fine until I tried to add routing. I read that Angularjs version 1.2+ requires 'ngRoute' as an dependency (I am using version 1.2.16). I added it but it still doesn't work. Below are my codes.

test.html (Main Page)

<html ng-app="demoApp">
<head>
    <title></title>
</head>
<body>
    <p>Front Page</p>
    <div ng-view></div>
    <script src="angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
    <script src="testjs.js"></script>
</body>
</html> 

testjs.js

demoApp = angular.module('demoApp',['ngRoute']);

demoApp.config(function ($routeProvider) {

    $routeProvider.when('/', {
        controller: 'SimpleController',
        templateUrl: '/partials/first.html'
    });

});

var controllers = {};
controllers.SimpleController = function ($scope){
    $scope.first = "Info";
    $scope.customers=[
        {name:'jerry',city:'chicago'},
        {name:'tom',city:'houston'},
        {name:'enslo',city:'taipei'}
    ];
};
demoApp.controller(controllers);

first.html

<div>
    <input type="text" ng-model="name"/>
    </br>
    {{first}}
    </br> 
    <ul>
        <li ng-repeat="cust in customers | filter:name">{{cust.name | uppercase}} - {{cust.city}}</li>
    </ul>   
</div>
Rodman answered 17/4, 2014 at 23:34 Comment(8)
There is a link to a picture of my working directory: postimg.org/image/979tsaecdRodman
are you sure this is correct syntax: demoApp.controller(controllers) ? I can't find any reference to it.Underarm
yes, I saw it in this tutorial: youtube.com/watch?v=Uj-KLCTsQrw .Rodman
I will try to find the exact video time where I found the code. give me a second.Rodman
sorry it's actually in this video: youtube.com/watch?v=rAyEGv67P-U . And it is 10 minutes and 21 seconds into the videoRodman
and everything worked fine until I tried to do routing with .configRodman
do you get any error in the console?Trip
I didn't run it through the console. I simply opened with chrome. I just found out that I had the error "XMLHttpRequest cannot load file:///partials/first.html. Cross origin requests are only supported for HTTP." when running on chrome. I am going to try to fix it. Thanks for now!Rodman
U
11

Here is the most basic setup possble, I'll try to make another one with your code: http://plnkr.co/edit/sN9TagVBOdX3mkrxaTiu?p=preview

EDIT updated with the sample code. Everything seems to be working?

EDIT 2 the problem is OP wasn't running a webserver. Ng-Route needs a webserver to function properly.

Underarm answered 17/4, 2014 at 23:54 Comment(9)
Thanks! I will try to see what's wrong with my codes. Thank you though!Rodman
apparently I got this error "XMLHttpRequest cannot load file:///partials/first.html. Cross origin requests are only supported for HTTP." I will try to fix it now. Thanks!Rodman
try using partials/first.html (without slash)Underarm
still the same result. I think I found the answer though: #19847752 . Really appreciated ur help.Rodman
that's not a really good answer. Is it possible you aren't running a webserver? That you are just viewing static html on your filesystem? If so, then there is your problem. Ng-Route needs a webserver.Underarm
that's exactly the case. Everything works once I run it on a webserverRodman
Another mystery solved :-) for local development you could use grunt to boot up a server :-)Underarm
I'v tried the same thing on tomcat server but its not working. some error showing like 1> Error: Unknown provider: $sceProvider <- $sce <- $route <- ngViewDirective and other one is 2> Circular dependency: ngViewDirectiveTrenton
that's a different question.Underarm
O
8

My routing wasn't working because there was an exclamation point inserted in the url when I tried to navigate to my routes. I added $locationProvider like this

app.config(function($routeProvider, $locationProvider) {
    $locationProvider.hashPrefix('');

to remove the exclamation point and my template views started appearing when I navigated to them. I found the answer here Exclamation mark after hash (#!) in angularjs app

Odelsting answered 31/5, 2017 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.