-
Notifications
You must be signed in to change notification settings - Fork 127
/
title.js
35 lines (31 loc) · 1006 Bytes
/
title.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import angular from 'angular'
module.exports = angular.module('title', [])
.directive('title', function($document, $timeout) {
const title = angular.element($document[0].querySelector('title')).clone()
function syncTitle() {
// $timeout is needed to get pushState + history + title to play correctly
// together. otherwise, the browser ends up recording weird page titles in
// history for some reason.
$timeout(function() {
$document[0].title = title.text()
})
}
return {
restrict: 'E',
transclude: true,
link: function(scope, elem, attrs, controller, transclude) {
transclude(function(clone, scope) {
title.append(clone)
scope.$on('$destroy', function() {
clone.remove()
syncTitle()
})
scope.$watch(function() { return clone.text() }, function(current) {
if (current.indexOf('{{') === -1) // prevent unwanted/early updates
syncTitle()
})
})
}
}
})
.name