AngularJS Example

for http://stepansuvorov.com/blog/

by Stepan Suvorov

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<div ng-controller="userController">
    <h1>{{name.first + ' ' + name.last}}</h1>    
</div>

JavaScript

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

app.controller('userController', function ($scope, nameStorage) {    
    $scope.name = nameStorage.name;
})
    .service('nameStorage', function ($timeout) {
    this.name = {
        first: 'Alice',
        last: 'Green'
    };
    
    //just to inimitate sevice change    
    var self = this;   
    $timeout(function () {
        self.name.first = 'Bob';
    }, 2000);
});