@@ -7,80 +7,86 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
7
7
var directive = {
8
8
restrict : 'ECA' ,
9
9
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 ) ;
16
17
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 ) ) ;
24
20
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 ) ;
27
28
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 ) ;
31
31
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
48
35
49
- var contents ;
36
+ // Remove existing content
50
37
if ( animate && doAnimate ) {
51
- contents = angular . element ( '<div></div>' ) . html ( locals . $template ) . contents ( ) ;
52
- animate . enter ( contents , element ) ;
38
+ animate . leave ( element . contents ( ) , element ) ;
53
39
} else {
54
- element . html ( locals . $template ) ;
55
- contents = element . contents ( ) ;
40
+ element . html ( '' ) ;
56
41
}
57
42
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 ;
64
47
}
65
- link ( viewScope ) ;
66
- viewScope . $emit ( '$viewContentLoaded' ) ;
67
- viewScope . $eval ( onloadExp ) ;
68
48
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 ;
75
52
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 ( ) ;
79
76
} 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
+ }
81
87
}
82
88
}
83
- }
89
+ } ;
84
90
}
85
91
} ;
86
92
return directive ;
0 commit comments