0

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you and a very useful link!
Here you can get another useful Angular style guide
2

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);

7 Comments

Updated the code plnkr.co/edit/KyMnxW?p=preview but it doesn't seem to be it. Still not working
@Norfeldt check the edit which has been done by Kostya Shkryob Thanks man for edit
Thx a lot for finding all these typos. Updated the code. But still not working or throwing any errors. plnkr.co/edit/KyMnxW?p=preview
I have made working version of plunker plnkr.co/edit/UV4Wzqx4vEsRdWAAMslE?p=preview
@KostyaShkryob Thanks for finding and appreciate your contribution to this answer.. :)
|
0

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", ...)
 }

1 Comment

No..its not what you are saying..OP has declared module only once & then he is utilizing it using angular.module('githubViewer')

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.