0

I am still in study mode of angularjs and just 2 day old. I was trying to make module and so i created seperate js file for it and created module like below. Also added controller.

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

But when i run i get error 'MainCtrl' is not a function, got undefined

here is Plunker

Can someone help me?

1
  • 2
    You have a module plunker already. Why did you want to create this one, what for? Commented Sep 1, 2015 at 11:28

3 Answers 3

1

After looking in plunker,I think you want to create a separate module in separate file for your controllers and add it to your main module.

For that create module for controllers in separate file,

angular.module("githubViewer", [])
.controller('MainCtrl', function($scope,$http) {
//your logic
});

then add it to your main as dependency in main module

angular.module('plunker', ['githubViewer']);

here is working demo : http://plnkr.co/edit/T9p7Uo2DxUVjqS1wuuiA?p=preview

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

5 Comments

what i can write inside app.controller("mainctrl)..... i have written logic of my controller in seperate file
what do you mean by seperate file? different controller module?
yes i have logic of controller in seperate js file and module in seperate file
Just add that module as dependency to you main module for example var ctrlModule = angular.module("ctrlModule", []); then in your main module do like this var app = angular.module("mainModule", ['ctrlModule']);
i think your looking for this plnkr.co/edit/T9p7Uo2DxUVjqS1wuuiA?p=preview
0

Ok, you're new to angular, so here's a couple of rules which you must follow until you can prove you need to do otherwise.

  1. You can place definition of module in a separate file. In short plunkers it is often an overkill, but that's what you should be doing in realworld-sized apps. Note that I'm talking about only the module here. Not talking about controllers, factories and other stuff.

  2. Separating body of controller from its inclusion into angular does not bring any benefit. Don't do that.

That said, your files should look like this:

# my_app.module.js
angular.module('myApp', []);

# main.controller.js
var app = angular.module('myApp')
app.controller('MainCtrl', MainCtrl);

function MainCtrl() {
  // logic here
}

Comments

0

I check your Plunker.

here is Working Plunker as you want logic of controller in seperate js file and module in seperate file

app.js

function MainCtrl($scope,$http) {
    var person = {
        firstName: "Kiran",
        lastName: "Nandedkar"
    };
    $scope.name = 'World';

    var onUserComplete = function(response){
        $scope.user = response.data;
    }

    var onError = function(reason){
       $scope.error = "dfdfdf"  ;
    }

    $http.get("https://api.github.com/users/odetocode")
      .then(onUserComplete,onError);

    $scope.person = person;
};

module.js

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

index.html

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

    <head>
        <meta charset="utf-8" />
        <title>AngularJS Plunker</title>
        <script>document.write('<base href="' + document.location + '" />');</script>
        <link rel="stylesheet" href="style.css" />
        <script data-require="[email protected]" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
        <script src="app.js"></script>
        <script src="module.js"></script>
  </head>

  <body ng-controller="MainCtrl">
      <p>Hello {{person.firstName}}!</p>
      <div>Login : {{user.login}}</div>
 </body>

Comments

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.