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>
yellowbyte
  • 189
  • 1
  • 2
  • 13
  • There is a link to a picture of my working directory: http://postimg.org/image/979tsaecd/ – yellowbyte Apr 17 '14 at 23:37
  • are you sure this is correct syntax: demoApp.controller(controllers) ? I can't find any reference to it. – Yoeri Apr 17 '14 at 23:40
  • yes, I saw it in this tutorial: https://www.youtube.com/watch?v=Uj-KLCTsQrw . – yellowbyte Apr 17 '14 at 23:44
  • I will try to find the exact video time where I found the code. give me a second. – yellowbyte Apr 17 '14 at 23:45
  • sorry it's actually in this video: https://www.youtube.com/watch?v=rAyEGv67P-U . And it is 10 minutes and 21 seconds into the video – yellowbyte Apr 17 '14 at 23:47
  • and everything worked fine until I tried to do routing with .config – yellowbyte Apr 17 '14 at 23:48
  • do you get any error in the console? – klode Apr 17 '14 at 23:51
  • 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! – yellowbyte Apr 18 '14 at 00:01

2 Answers2

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.

Yoeri
  • 2,249
  • 18
  • 33
  • Thanks! I will try to see what's wrong with my codes. Thank you though! – yellowbyte Apr 17 '14 at 23:59
  • 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! – yellowbyte Apr 18 '14 at 00:03
  • try using partials/first.html (without slash) – Yoeri Apr 18 '14 at 00:04
  • still the same result. I think I found the answer though: http://stackoverflow.com/questions/19847252/cross-origin-requests-are-only-supported-for-http . Really appreciated ur help. – yellowbyte Apr 18 '14 at 00:09
  • 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. – Yoeri Apr 18 '14 at 00:18
  • 1
    that's exactly the case. Everything works once I run it on a webserver – yellowbyte Apr 18 '14 at 00:21
  • Another mystery solved :-) for local development you could use grunt to boot up a server :-) – Yoeri Apr 18 '14 at 00:23
  • 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: ngViewDirective – userBI Jul 28 '14 at 18:37
  • that's a different question. – Yoeri Jul 29 '14 at 09:03
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

gary69
  • 3,620
  • 6
  • 36
  • 50