1

I have just started an Angularjs tutorial and I fail in running the exercise from the first lesson. It is about creating 2 modules: the first is the bootstraper of the application and the second one contains 2 controllers.

This is the code:

app.js

'use strict';

angular.module('myApp', ['myApp.controllers'])
    .run(function ($rootScope) {
        $rootScope.title = 'Famouse books';
        $rootScope.name = 'Root Scope';
});

controllers.js

angular.module('myApp.controllers', []).controller('SiteController', function ($scope) {
    $scope.publisher = 'Site point';
    $scope.type = 'Web development';
    $scope.name = 'Scope for SiteController';
});

angular.module('myApp.controllers', []).controller('BookController', function ($scope) {
    $scope.books = ['Jump Start HTML 5', 'Jump Start CSS', 'Jump Start Responsive Web Design'];
    $scope.name = 'Scope for BookController';
});

index.html

<!DOCTYPE html>
<html ng-app="myApp">
    <head>
        <title ng-bind="title"></title>
    </head>

    <body ng-controller="SiteController">
        <span>{{publisher}} excels in {{type}} books</span>
        <div ng-controller="BookController">
            <h3>Some of the popular books from {{publisher}}</h3>
            <ul>
                <li ng-repeat="book in books">{{book}}</li>
            </ul>
        </div>

        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
        <script src="scripts/app/controllers.js"></script>
        <script src="scripts/app/app.js"></script>
    </body>
</html>

I get the following error:

Bad Argument: Argument 'SiteController' is not a function, got undefined
1
  • angular.module('myApp.controllers', []) is setter (create) and angular.module('myApp.controllers') is getter (get already created one). Commented Dec 19, 2014 at 17:25

1 Answer 1

2

You added extra ,[] in second sentence. This creates a new module and therefore overwrites the first module that has "SiteController" defined - you want to add to the existing module:

angular.module('myApp.controllers', []).controller('SiteController', function ($scope) {
    $scope.publisher = 'Site point';
    $scope.type = 'Web development';
    $scope.name = 'Scope for SiteController';
});

// you added extra ,[]
angular.module('myApp.controllers').controller('BookController', function ($scope) {
    $scope.books = ['Jump Start HTML 5', 'Jump Start CSS', 'Jump Start Responsive Web Design'];
    $scope.name = 'Scope for BookController';
});

Calling angular.module('myApp.controllers', []) means: Create Module.

Calling angular.module('myApp.controllers') means: Get Module.

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

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.