AngularJS Example

for http://stepansuvorov.com/blog/

by Stepan Suvorov

HTML

<div ng-controller="mainCtrl">
</div>

JavaScript

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

app.service('helloWorldService', function(){
    this.hello = function() {
        return "Hello World";
    };
});
 
app.factory('helloWorldFactory', function(){
    return {
        hello: function() {
            return "Hello World";
        }
    }
});

app.factory('helloFactory', function() {
    return function(name) {
        this.name = name;
        this.hello = function() {
            return "Hello " + this.name;
        };
    };
});



app.controller('mainCtrl', function ($scope, helloWorldService,
                                     helloWorldFactory,
                                     helloFactory) {
    
    alert(helloWorldService.hello()); //'Hello World'
    alert(helloWorldFactory.hello()); //'Hello World'
    alert(new helloFactory('Readers').hello()) //'Hello Readers'
  
});