Last active
August 29, 2015 14:06
-
-
Save munro/213f245f6b31306d6997 to your computer and use it in GitHub Desktop.
AngularJS Window Title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title ng-bind="window_title"></title> | |
</head> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory('$windowTitle', function ($rootScope) { | |
var $windowTitle = {}; | |
function render() { | |
var title = $windowTitle.queue[0] && $windowTitle.queue[0].title; | |
if (typeof title === 'function') { | |
$rootScope.window_title = angular.element(document).injector().invoke(title); | |
} else if (typeof title === 'string') { | |
$rootScope.window_title = title; | |
} else { | |
$rootScope.window_title = ''; | |
} | |
} | |
$windowTitle.queue = []; | |
$windowTitle.add = function (str, priority) { | |
var obj = {title: str, priority: priority}; | |
$windowTitle.queue = $windowTitle.queue.concat([obj]).sort(function (a, b) { | |
return b.priority - a.priority; | |
}); | |
render(); | |
return function remove() { | |
$windowTitle.queue = $windowTitle.queue.filter(function (item) { | |
return item !== obj; | |
}); | |
render(); | |
}; | |
}; | |
$rootScope.$watch(function () { | |
render(); | |
}); | |
return $windowTitle; | |
}); | |
app.run(function ($windowTitle) { | |
$windowTitle.add('Default Title', 0); | |
}); | |
app.controller('MyController', function MessageUserListPageCtrl($scope, $windowTitle) { | |
var removeTitle = $windowTitle.add(function ($rootScope) { | |
return 'My Title ' + $rootScome.some_variable; | |
}, 10); | |
$scope.$on('$destroy', function () { | |
removeTitle(); | |
}); | |
}); | |
app.factory('$chatService', function ($scope, $windowTitle) { | |
var removeTitle; | |
var $events; | |
setInterval(function () { | |
var hadTitle = !!removeTitle; | |
if (removeTitle) { | |
removeTitle(); | |
removeTitle = null; | |
} | |
if ($events.newChatMessage && !hadTitle) { // makes it blink | |
removeTitle = $windowTitle.add('New chat message!!!', 20); | |
} | |
}, 2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment