broadcast replacement

$broadcast replacement by defineProperty

by Muhammad Irfan Khan

HTML

<script src="https://code.angularjs.org/1.3.4/angular.js"></script>
<div ng-app="myApp">
        <div ng-controller="header as hd">
                           User: {{hd.user}} <br /> 
                           department = {{vm.department}}
        </div>
        
        <div ng-controller="shell as sh"></div>
    </div>

JavaScript

(function (app) {

   

    function userService($timeout) {
        var user = '';
        var department = '';

        this.getUser = function () {
            return user;
        }
        this.getDepartment = function () {
            return department;
        }
        this.setUser = function () {
            wait(10).then(function () {
                console.log("wait then called after 10 seconds");
                user = "irfan";
                department = 'my department';
                department = "my department";
            })
        }

        function wait(seconds) {
            return $timeout(toBeCalled, seconds * 1000);
        }

        function toBeCalled() {
            console.log("to be called after 10 seconds");
        }
    }

    function header(userService) {
        this.userService = userService;
        this.department = userService.getDepartment();
    }

    Object.defineProperty(header.prototype,
        'user', {
            enumerable: true,
            configurable: false,
            get: function () {
                return this.userService.getUser();
            }
        });

    function shell(userService) {
        userService.setUser();
    }

   
    

    app.service('userService', userService);
    app.controller('header', header);
    app.controller('shell', shell);
    

})(angular.module('myApp', []));