-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathviewDirective.js
64 lines (55 loc) · 2.33 KB
/
viewDirective.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$ViewDirective.$inject = ['$state', '$compile', '$controller', '$anchorScroll'];
function $ViewDirective( $state, $compile, $controller, $anchorScroll) {
var directive = {
restrict: 'ECA',
terminal: true,
link: function(scope, element, attr) {
var viewScope, viewLocals,
name = attr[directive.name] || attr.name || '',
onloadExp = attr.onload || '';
// Find the details of the parent view directive (if any) and use it
// to derive our own qualified view name, then hang our own details
// off the DOM so child directives can find it.
var parent = element.parent().inheritedData('$uiView');
if (name.indexOf('@') < 0) name = name + '@' + (parent ? parent.state.name : '');
var view = { name: name, state: null };
element.data('$uiView', view);
scope.$on('$stateChangeSuccess', updateView);
updateView();
function updateView() {
var locals = $state.$current && $state.$current.locals[name];
if (locals === viewLocals) return; // nothing to do
// Destroy previous view scope (if any)
if (viewScope) {
viewScope.$destroy();
viewScope = null;
}
if (locals) {
viewLocals = locals;
view.state = locals.$$state;
element.html(locals.$template);
// element.html('<div style="height:0;position:relative;z-index:999"><span style="background:red;color:white;font-size:12px;padding:1px">' + name + '</span></div>' + locals.$template);
var link = $compile(element.contents());
viewScope = scope.$new();
if (locals.$$controller) {
locals.$scope = viewScope;
var controller = $controller(locals.$$controller, locals);
element.contents().data('$ngControllerController', controller);
}
link(viewScope);
viewScope.$emit('$viewContentLoaded');
viewScope.$eval(onloadExp);
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
// $anchorScroll might listen on event...
$anchorScroll();
} else {
viewLocals = null;
view.state = null;
element.html('');
}
}
}
};
return directive;
}
angular.module('ui.state').directive('uiView', $ViewDirective);