Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am absolutly new in AngularJS and I am following a course that show a first example of AngularJS application and I have some doubt about how exactly works.

So it is composed by these two files:

1) index.htm:

<!DOCTYPE html>
<html lang="en-us" ng-app="angularApp">
    <head>
        <title>Introduction to AngularJS</title>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta charset="UTF-8">

        <!-- load bootstrap and fontawesome via CDN -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
        <style>
            html, body
            {
                font-size: 1.1em;
            }
        </style>

        <!-- load angular via CDN -->
        <script src="//code.angularjs.org/1.3.0-rc.1/angular.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body>

        <header>
            <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">AngularJS</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                </ul>
            </div>
            </nav>
        </header>

        <div class="container">

            <!-- This div and its content is the view associated to the 'mainController': -->
            <div ng-controller="mainController">

                <h1>Hello world!</h1>

            </div>

        </div>

    </body>
</html>

2) app.js:

/* MODULE: one signgle object in the global namespace.
           Everything insise the element having ng-app="angularApp" custom attribute is connected to the angularApp variable into the
           global namespace
*/
var angularApp = angular.module('angularApp', []);

// CONTROLLERS
angularApp.controller('mainController', ['$scope', function ($scope) {

}]);

So I have some doubt about how exactly it works.

From what I have understand into the app.js file it is defined a module that substantially is a single object defined into the global namespace of my application. But what exactly represent a module in AngularJS? I came from the Spring MVC framework (and some other classic MVC) where the module represent the data that have to be shown into a view.

In Angular what exactly represent?

So from what I have understand this module is bound with the entire index.htm page because in the HTML is declared:

<html lang="en-us" ng-app="angularApp">

so I think that the ng-app="angularApp" on the html page means that the angularApp module is related to what happen inside the index.html page. Is it my reasoning correct or it is wrong?

Then inside the app.js file it is also defined a controller:

// CONTROLLERS
angularApp.controller('mainController', ['$scope', function ($scope) {

}]);

This mainController controller (is it mainController the controller name?) is defined on the angularApp model object.

Ok, I am not so into JavaScript but I think that works in the following way:

The mainController variable is a JavaScript object and by the previous line I am adding a controller field named mainController and the controller logic is implemented by the function associated to this new field (because in JavaScript a function could be a field of an object).

Is it my reasoning true or am I missing something about the controller declaration?

Another doubt is related to the use of the '$scope' variable. What exactly means? and what is the meanining of the syntax ['$scope', function ($scope) { ....CONTROLLER LOGIC....}] ?

This controller is associated to a specific view that is represented by a specific section of the index.htm page, this one:

        <!-- This div and its content is the view associated to the 'mainController': -->
        <div ng-controller="mainController">

            <h1>Hello world!</h1>

        </div>

And this generate some confusion in me because it seems pretty different from what I see in other Java MVC implementations.

So differently from Java MVC implementations, in AngularJS a view is not an entire page (as in Java could be a .jsp page) but it is a section of an HTML page bound to a specific controller by the ng-controller="mainController" custom attribute. Is it true?

And also differently from Java MVC implementations, in AngularJS (or in the previous example) a model is not only an object that contain valorized fields to display into a view but it is an object in the global namespace of the application to which are associated the controllers (as model's fields) that perform the operation on the specified view.

Are my reasoning correct or am I missing something?

share|improve this question
1  
don't confuse module and model. – Kevin B Nov 3 at 19:52
    
@KevinB please can you give me more information :-) – AndreaNobili Nov 3 at 19:53
    
I don't think i could provide the amount of information you need in an answer. – Kevin B Nov 3 at 19:54
    
this is really not a question that can be adequately addressed in a single answer without a lot of clarifying information going back and forth. I would encourage you to jump into chat instead; There is almost always discussions going on in the JavaScript channel, but I'll hang out in the angular channel for a bit to have this specific discussion with you. – Claies Nov 3 at 22:55

1 Answer 1

up vote 1 down vote accepted

Everything is fine, your controller and your view is ok. Next step, you need to create now a service to bind a server API and act like your model.

In fact, the angular 'controller' can act like the model but if you want to do only controller/listeners things in there, you need to create a real service like this:

exemple:

app.service('ContactService', function () {

//'var' act like private
var contacts = [{
    id: 0,
    name: 'hello world'
}];

//'this' act like public (cool for java dev, you should feel like home :) )
this.save = function (contact) {
    //here connect to your api or stay offline
}

this.get = function (id) {

}

this.delete = function (id) {

}

this.list = function () {
    return contacts;
}
});

your controller

angularApp.controller('mainController', ['$scope','ContactService', function ($scope,ContactService) {
    $scope.contact = ContactService.get(0)
}]);

the view

    <div ng-controller="mainController">

        <h1>{{contact.name}}</h1>

    </div>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.