Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit aa2133a

Browse files
matskomhevery
authored andcommitted
fix(ngInclude): $animate refactoring + use transclusion
BREAKING CHANGE: previously ngInclude only updated its content, after this change ngInclude will recreate itself every time a new content is included. This ensures that a single rootElement for all the included contents always exists, which makes definition of css styles for animations much easier.
1 parent 8ed0d5b commit aa2133a

File tree

4 files changed

+105
-70
lines changed

4 files changed

+105
-70
lines changed

docs/src/templates/css/animations.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
overflow:hidden;
1616
}
1717

18-
.slide-reveal > .ng-enter {
18+
.slide-reveal.ng-enter {
1919
-webkit-transition:0.5s linear all;
2020
-moz-transition:0.5s linear all;
2121
-o-transition:0.5s linear all;
@@ -26,7 +26,7 @@
2626
opacity:0;
2727
top:10px;
2828
}
29-
.slide-reveal > .ng-enter.ng-enter-active {
29+
.slide-reveal.ng-enter.ng-enter-active {
3030
top:0;
3131
opacity:1;
3232
}

src/ng/directive/ngInclude.js

+44-38
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
* access on some browsers)
2525
*
2626
* @animations
27-
* enter - happens just after the ngInclude contents change and a new DOM element is created and injected into the ngInclude container
28-
* leave - happens just after the ngInclude contents change and just before the former contents are removed from the DOM
27+
* enter - animation is used to bring new content into the browser.
28+
* leave - animation is used to animate existing content away.
29+
*
30+
* The enter and leave animation occur concurrently.
2931
*
3032
* @scope
3133
*
@@ -49,9 +51,9 @@
4951
</select>
5052
url of the template: <tt>{{template.url}}</tt>
5153
<hr/>
52-
<div class="example-animate-container"
53-
ng-include="template.url"
54-
ng-animate="{enter: 'example-enter', leave: 'example-leave'}"></div>
54+
<div class="example-animate-container">
55+
<div class="include-example" ng-include="template.url"></div>
56+
</div>
5557
</div>
5658
</file>
5759
<file name="script.js">
@@ -63,14 +65,13 @@
6365
}
6466
</file>
6567
<file name="template1.html">
66-
<div>Content of template1.html</div>
68+
Content of template1.html
6769
</file>
6870
<file name="template2.html">
69-
<div>Content of template2.html</div>
71+
Content of template2.html
7072
</file>
7173
<file name="animations.css">
72-
.example-leave,
73-
.example-enter {
74+
.include-example.ng-enter, .include-example.ng-leave {
7475
-webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
7576
-moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
7677
-ms-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
@@ -82,24 +83,21 @@
8283
left:0;
8384
right:0;
8485
bottom:0;
85-
}
86-
87-
.example-animate-container > * {
8886
display:block;
8987
padding:10px;
9088
}
9189
92-
.example-enter {
90+
.include-example.ng-enter {
9391
top:-50px;
9492
}
95-
.example-enter.example-enter-active {
93+
.include-example.ng-enter.ng-enter-active {
9694
top:0;
9795
}
9896
99-
.example-leave {
97+
.include-example.ng-leave {
10098
top:0;
10199
}
102-
.example-leave.example-leave-active {
100+
.include-example.ng-leave.ng-leave-active {
103101
top:50px;
104102
}
105103
</file>
@@ -115,7 +113,7 @@
115113
});
116114
it('should change to blank', function() {
117115
select('template').option('');
118-
expect(element('.doc-example-live [ng-include]').text()).toEqual('');
116+
expect(element('.doc-example-live [ng-include]')).toBe(undefined);
119117
});
120118
</file>
121119
</example>
@@ -145,21 +143,26 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
145143
return {
146144
restrict: 'ECA',
147145
terminal: true,
148-
compile: function(element, attr) {
146+
transclude: 'element',
147+
compile: function(element, attr, transclusion) {
149148
var srcExp = attr.ngInclude || attr.src,
150149
onloadExp = attr.onload || '',
151150
autoScrollExp = attr.autoscroll;
152151

153-
return function(scope, element, attr) {
152+
return function(scope, $element) {
154153
var changeCounter = 0,
155-
childScope;
154+
currentScope,
155+
currentElement;
156156

157-
var clearContent = function() {
158-
if (childScope) {
159-
childScope.$destroy();
160-
childScope = null;
157+
var cleanupLastIncludeContent = function() {
158+
if (currentScope) {
159+
currentScope.$destroy();
160+
currentScope = null;
161+
}
162+
if(currentElement) {
163+
$animate.leave(currentElement);
164+
currentElement = null;
161165
}
162-
$animate.leave(element.contents());
163166
};
164167

165168
scope.$watch($sce.parseAsResourceUrl(srcExp), function ngIncludeWatchAction(src) {
@@ -168,28 +171,31 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
168171
if (src) {
169172
$http.get(src, {cache: $templateCache}).success(function(response) {
170173
if (thisChangeId !== changeCounter) return;
174+
var newScope = scope.$new();
171175

172-
if (childScope) childScope.$destroy();
173-
childScope = scope.$new();
174-
$animate.leave(element.contents());
176+
transclusion(newScope, function(clone) {
177+
cleanupLastIncludeContent();
175178

176-
var contents = jqLite('<div/>').html(response).contents();
179+
currentScope = newScope;
180+
currentElement = clone;
177181

178-
$animate.enter(contents, element);
179-
$compile(contents)(childScope);
182+
currentElement.html(response);
183+
$animate.enter(currentElement, null, $element);
184+
$compile(currentElement.contents())(currentScope);
180185

181-
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
182-
$anchorScroll();
183-
}
186+
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
187+
$anchorScroll();
188+
}
184189

185-
childScope.$emit('$includeContentLoaded');
186-
scope.$eval(onloadExp);
190+
currentScope.$emit('$includeContentLoaded');
191+
scope.$eval(onloadExp);
192+
});
187193
}).error(function() {
188-
if (thisChangeId === changeCounter) clearContent();
194+
if (thisChangeId === changeCounter) cleanupLastIncludeContent();
189195
});
190196
scope.$emit('$includeContentRequested');
191197
} else {
192-
clearContent();
198+
cleanupLastIncludeContent();
193199
}
194200
});
195201
};

src/ngAnimate/animate.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
* transition:0.5s linear all;
4848
* }
4949
*
50-
* .slide > .ng-enter { } /&#42; starting animations for enter &#42;/
51-
* .slide > .ng-enter-active { } /&#42; terminal animations for enter &#42;/
52-
* .slide > .ng-leave { } /&#42; starting animations for leave &#42;/
53-
* .slide > .ng-leave-active { } /&#42; terminal animations for leave &#42;/
50+
* .slide.ng-enter { } /&#42; starting animations for enter &#42;/
51+
* .slide.ng-enter-active { } /&#42; terminal animations for enter &#42;/
52+
* .slide.ng-leave { } /&#42; starting animations for leave &#42;/
53+
* .slide.ng-leave-active { } /&#42; terminal animations for leave &#42;/
5454
* </style>
5555
*
5656
* <!--

0 commit comments

Comments
 (0)