Norfeldt
Norfeldt

Reputation: 9678

AngularJS routing not working, but no errors are thrown

I'm following this video tutorial and messed something up in the haste.. The problem is that I got rid of all the errors I made, but it still doesn't work as expected. The search form box doesn't display.

Full code can be found here: plnkr code

index.html

<!DOCTYPE html>
<html ng-app="githubViewer">

  <head>
    <script data-require="[email protected]" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
    <script data-require="angular-route@*" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular-route.js"></script>
    <link rel="stylesheet" href="style.css" />

    <script scr="app.js"></script>
    <script src="MainController.js"></script>
    <script src="github.js"></script>
  </head>

  <body >
    <h1>Gihub Viewer</h1>
    <div ng-view></div>
  </body>

</html>

app.js

(function(){

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

  app.config(function($routeProvider){
    $routeProvider
      .when("/main", {
        templateUrl: "main.html",
        controller: "MainController"
        })
      .otherwise({redirectTo:"/main"});
  });

}());

main.html

<div>
  {{ countdown }}
  <form name="searchUser" ng-submit="search(username)">
    <input type="search" required="" placeholder="Username to find" 
      ng-model="username" />
    <input type="submit" value="Search" />
  </form>
</div>

Any help would be appreciated

Upvotes: 0

Views: 4313

Answers (3)

Carlos Mayo
Carlos Mayo

Reputation: 2124

The problem is that you are redefinig the module in the controller and factory code:

var app = angular.module("githubViewer",[]);

Instead of gettingthe created module:

var app = angular.module("githubViewer");

A module should be created once, then retrieve and add the controller, config, factory, etc...

Here some help.

Upvotes: 2

Andrew Shepherd
Andrew Shepherd

Reputation: 45252

You are misunderstanding how to implement a module and a controller. In both MainController.js and app.js, you have the line:

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

angular.module actually creates a new module. You are effectively trying to create two modules with the same name, which isn't allowed.

The pattern to follow is

var app = angular.module(...)

app.config(...)

var controller = app.controller("MainController", ["$scope", function() {
 ]]);

If you really want the MainController code to be in a separate file, create a function:

 function createMainController(app) {
      app.contoller("MainController", ...)
 }

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

Basically you all js file has wrong IIFE pattern.wrong

It should be

})();

instead of

}());

Also there are typos. In index.html:

<script scr="app.js"></script>

Must be:

<script src="app.js"></script>

And in MainController.js. This code:

app.controller("MainControler", MainController);

Must be changed to:

app.controller("MainController", MainController);

Upvotes: 2

Related Questions