0

I'm trying to make an AngularJS application but I don't understand why it doesn't route correctly, I followed some guides but haven´t made it work yet.

Here is the code:

index.html

<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>Example App</title>
</head>
<body>

    <div ng-view></div>

    <!-- Vendor libraries -->
    <script src="lib/jquery-2.1.4.min.js"></script>
    <script src="lib/angular.min.js"></script>
    <script src="lib/angular-route.min.js"></script>
    <!-- Application Files -->
    <script src="app/app.js"></script>
    <script src="app/home/controllers/HomeController.js"></script>
</body>
</html>

app.js

(function() {
    'use strict';

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

    app.config(function($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'home/views/index.html',
                controller: 'HomeController',
                controllerAs: 'home'
            })
            .otherwise({
                redirectTo: '/'
            });
    });
})();

HomeController.js

(function() {
    'use strict';

    angular
       .module('App')
       .controller('HomeController', HomeController);

    function HomeController(){
        var home = this;
    }
})();

home's index.html

<div>
   Example text
</div>

Thanks in advance

8
  • Perhaps you can post the most important part of your code here as a snippet instead of referring to an external link? This post will be out-of-date as soon as the link doesn't work anymore. Commented Nov 24, 2015 at 22:25
  • @BryanOemar Thanks for the advise, I have changed that. Commented Nov 24, 2015 at 22:39
  • I don't think you need to be wrapping your code in immediately executing functions. The fact you have included the ng-app attribute means angular will execute your code when required Commented Nov 24, 2015 at 22:45
  • This works properly in a plunker (only changing the name of the home index to a plunker friendly format). If this is not working in your environment, there is something wrong that you are not showing here. Commented Nov 24, 2015 at 23:12
  • If I had to guess, based on what you have described here so far, and the only change that I made in the example plunker, I would imagine that your templateUrl path isn't valid. Commented Nov 24, 2015 at 23:14

1 Answer 1

2

Are you seeing errors in the console? You're probably getting an error relating to the controller not being found, as 'home/controllers/HomeController' is not a proper way to reference HomeController. It should instead read: 'HomeController'. Angular will do the work to traverse your controllers and find one that matches that string.

Here's an updated app.js:

(function() {
    'use strict';

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

    app.config(function($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'home/views/index.html',
                controller: 'HomeController',
                controllerAs: 'home'
            })
            .otherwise({
                redirectTo: '/'
            });
    });
})();

Additionally, in HomeController.js, you are re-instantiating the module 'App' by including the brackets after the name. Instead, modify it to more succinctly read:

(function() {
    'use strict';

    angular
       .module('App')
       .controller('HomeController', HomeController);

    function HomeController(){
        var home = this;
    }
})();
Sign up to request clarification or add additional context in comments.

3 Comments

I see no error in console, I made the changes you suggested and still not seeing anything.
Are both angular and angular-route loading properly?
Yes they are, I found thanks to Claies that the templateUrl was not working properly. But your suggestions helped me a lot too. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.