<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', []));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.