Skip to content

Commit 6d063c0

Browse files
committed
fixed issue angular-ui#435
1 parent d280a9c commit 6d063c0

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/viewDirective.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
99
var directive = {
1010
restrict: 'ECA',
1111
terminal: true,
12+
priority: 1000,
1213
transclude: true,
1314
compile: function (element, attr, transclude) {
1415
return function(scope, element, attr) {
1516
var viewScope, viewLocals,
1617
name = attr[directive.name] || attr.name || '',
1718
onloadExp = attr.onload || '',
18-
animate = isDefined($animator) && $animator(scope, attr);
19+
animate = isDefined($animator) && $animator(scope, attr),
20+
initialView = transclude(scope);
1921

2022
// Returns a set of DOM manipulation functions based on whether animation
2123
// should be performed
@@ -42,7 +44,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
4244
};
4345

4446
// Put back the compiled initial view
45-
element.append(transclude(scope));
47+
element.append(initialView);
4648

4749
// Find the details of the parent view directive (if any) and use it
4850
// to derive our own qualified view name, then hang our own details
@@ -86,7 +88,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
8688
view.state = null;
8789

8890
// Restore the initial view
89-
return render.restore(transclude(scope), element);
91+
return render.restore(initialView, element);
9092
}
9193

9294
viewLocals = locals;

test/viewDirectiveSpec.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ describe('uiView', function () {
5050
template: 'hState inner template'
5151
}
5252
}
53+
},
54+
iState = {
55+
template: '<div ui-view>'+
56+
'<ul><li ng-repeat="item in items">{{item}}</li></ul>'+
57+
'</div>'
58+
},
59+
jState = {
60+
template: '<span ng-class="test">jState</span>'
5361
};
5462

5563
beforeEach(module(function ($stateProvider) {
@@ -61,7 +69,9 @@ describe('uiView', function () {
6169
.state('e', eState)
6270
.state('e.f', fState)
6371
.state('g', gState)
64-
.state('g.h', hState);
72+
.state('g.h', hState)
73+
.state('i', iState)
74+
.state('j', jState);
6575
}));
6676

6777
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -153,6 +163,41 @@ describe('uiView', function () {
153163

154164
expect(elem[0].querySelector('.test').innerText).toBe(content);
155165
}));
166+
167+
// related to issue #435
168+
it('initial view should be transcluded once to prevent breaking other directives', inject(function ($state, $q) {
169+
scope.items = ["I", "am", "a", "list", "of", "items"];
170+
171+
elem.append($compile('<div ui-view></div>')(scope));
172+
173+
// transition to state that has an initial view
174+
$state.transitionTo(iState);
175+
$q.flush();
176+
177+
// verify if ng-repeat has been compiled
178+
expect(elem.find('li').length).toBe(scope.items.length);
179+
180+
// transition to another state that replace the initial content
181+
$state.transitionTo(jState);
182+
$q.flush();
183+
184+
expect(elem.text()).toBe('jState');
185+
186+
// transition back to the state with empty subview and the initial view
187+
$state.transitionTo(iState);
188+
$q.flush();
189+
190+
// verify if the initial view is correct
191+
expect(elem.find('li').length).toBe(scope.items.length);
192+
193+
// change scope properties
194+
scope.$apply(function () {
195+
scope.items.push(".", "Working?");
196+
});
197+
198+
// verify if the initial view has been updated
199+
expect(elem.find('li').length).toBe(scope.items.length);
200+
}));
156201
});
157202

158203
});

0 commit comments

Comments
 (0)