Skip to content

Commit 87cf3de

Browse files
committed
transcluding the content of the ui-view directive
1 parent 528ea2f commit 87cf3de

File tree

1 file changed

+65
-59
lines changed

1 file changed

+65
-59
lines changed

src/viewDirective.js

+65-59
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,86 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
77
var directive = {
88
restrict: 'ECA',
99
terminal: true,
10-
link: function(scope, element, attr) {
11-
var viewScope, viewLocals,
12-
initialContent = element.contents(),
13-
name = attr[directive.name] || attr.name || '',
14-
onloadExp = attr.onload || '',
15-
animate = isDefined($animator) && $animator(scope, attr);
10+
transclude: true,
11+
compile: function (element, attr, transclude) {
12+
return function(scope, element, attr) {
13+
var viewScope, viewLocals,
14+
name = attr[directive.name] || attr.name || '',
15+
onloadExp = attr.onload || '',
16+
animate = isDefined($animator) && $animator(scope, attr);
1617

17-
// Find the details of the parent view directive (if any) and use it
18-
// to derive our own qualified view name, then hang our own details
19-
// off the DOM so child directives can find it.
20-
var parent = element.parent().inheritedData('$uiView');
21-
if (name.indexOf('@') < 0) name = name + '@' + (parent ? parent.state.name : '');
22-
var view = { name: name, state: null };
23-
element.data('$uiView', view);
18+
// Put back the compiled initial view
19+
element.append(transclude(scope));
2420

25-
scope.$on('$stateChangeSuccess', function() { updateView(true); });
26-
updateView(false);
21+
// Find the details of the parent view directive (if any) and use it
22+
// to derive our own qualified view name, then hang our own details
23+
// off the DOM so child directives can find it.
24+
var parent = element.parent().inheritedData('$uiView');
25+
if (name.indexOf('@') < 0) name = name + '@' + (parent ? parent.state.name : '');
26+
var view = { name: name, state: null };
27+
element.data('$uiView', view);
2728

28-
function updateView(doAnimate) {
29-
var locals = $state.$current && $state.$current.locals[name];
30-
if (locals === viewLocals) return; // nothing to do
29+
scope.$on('$stateChangeSuccess', function() { updateView(true); });
30+
updateView(false);
3131

32-
// Remove existing content
33-
if (animate && doAnimate) {
34-
animate.leave(element.contents(), element);
35-
} else {
36-
element.html('');
37-
}
38-
39-
// Destroy previous view scope
40-
if (viewScope) {
41-
viewScope.$destroy();
42-
viewScope = null;
43-
}
44-
45-
if (locals) {
46-
viewLocals = locals;
47-
view.state = locals.$$state;
32+
function updateView(doAnimate) {
33+
var locals = $state.$current && $state.$current.locals[name];
34+
if (locals === viewLocals) return; // nothing to do
4835

49-
var contents;
36+
// Remove existing content
5037
if (animate && doAnimate) {
51-
contents = angular.element('<div></div>').html(locals.$template).contents();
52-
animate.enter(contents, element);
38+
animate.leave(element.contents(), element);
5339
} else {
54-
element.html(locals.$template);
55-
contents = element.contents();
40+
element.html('');
5641
}
5742

58-
var link = $compile(contents);
59-
viewScope = scope.$new();
60-
if (locals.$$controller) {
61-
locals.$scope = viewScope;
62-
var controller = $controller(locals.$$controller, locals);
63-
element.children().data('$ngControllerController', controller);
43+
// Destroy previous view scope
44+
if (viewScope) {
45+
viewScope.$destroy();
46+
viewScope = null;
6447
}
65-
link(viewScope);
66-
viewScope.$emit('$viewContentLoaded');
67-
viewScope.$eval(onloadExp);
6848

69-
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
70-
// $anchorScroll might listen on event...
71-
$anchorScroll();
72-
} else {
73-
viewLocals = null;
74-
view.state = null;
49+
if (locals) {
50+
viewLocals = locals;
51+
view.state = locals.$$state;
7552

76-
// Restore initial view
77-
if (doAnimate) {
78-
animate.enter(initialContent, element);
53+
var contents;
54+
if (animate && doAnimate) {
55+
contents = angular.element('<div></div>').html(locals.$template).contents();
56+
animate.enter(contents, element);
57+
} else {
58+
element.html(locals.$template);
59+
contents = element.contents();
60+
}
61+
62+
var link = $compile(contents);
63+
viewScope = scope.$new();
64+
if (locals.$$controller) {
65+
locals.$scope = viewScope;
66+
var controller = $controller(locals.$$controller, locals);
67+
element.children().data('$ngControllerController', controller);
68+
}
69+
link(viewScope);
70+
viewScope.$emit('$viewContentLoaded');
71+
viewScope.$eval(onloadExp);
72+
73+
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
74+
// $anchorScroll might listen on event...
75+
$anchorScroll();
7976
} else {
80-
element.html(initialContent);
77+
viewLocals = null;
78+
view.state = null;
79+
80+
// Restore the initial view
81+
var compiledElem = transclude(scope);
82+
if (animate && doAnimate) {
83+
animate.enter(compiledElem, element);
84+
} else {
85+
element.append(compiledElem);
86+
}
8187
}
8288
}
83-
}
89+
};
8490
}
8591
};
8692
return directive;

0 commit comments

Comments
 (0)