Skip to content

Commit c2ca74a

Browse files
committed
a segment with undefined template now works with view-segment tag's body properly, so that you can inline a template right into top-level view
1 parent 7d17472 commit c2ca74a

File tree

5 files changed

+110
-25
lines changed

5 files changed

+110
-25
lines changed

example/app.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ app.config(function($routeSegmentProvider, $routeProvider) {
8989
.when('/invalid-data', 's1.invalidData')
9090
.when('/slow-data', 's1.slowDataSimple')
9191
.when('/slow-data-loading', 's1.slowDataLoading')
92+
.when('/inline-view', 's1.inlineParent.inlineChildren')
9293
.when('/section1/:id/slow', 's1.itemInfo.tabSlow')
9394

9495
.within('s1')
@@ -133,6 +134,23 @@ app.config(function($routeSegmentProvider, $routeProvider) {
133134
templateUrl: 'templates/loading.html'
134135
}
135136
})
137+
.segment('inlineParent', {
138+
templateUrl: 'templates/section1/inline-view.html'
139+
})
140+
.within()
141+
.segment('inlineChildren', {
142+
// no template here
143+
controller: 'SlowDataCtrl',
144+
resolve: {
145+
data: function($timeout) {
146+
return $timeout(function() { return 'SLOW DATA CONTENT'; }, 2000);
147+
}
148+
},
149+
untilResolved: {
150+
templateUrl: 'templates/loading.html'
151+
}
152+
})
153+
.up()
136154

137155
.within('itemInfo')
138156
.segment('tabSlow', {

example/templates/section1.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
<li ng-class="{active: $routeSegment.startsWith('s1.slowDataLoading')}">
3838
<a href="#/slow-data-loading">With `untilResolved`</a>
3939
</li>
40+
41+
<li ng-class="{active: $routeSegment.startsWith('s1.inlineView')}">
42+
<a href="#/inline-view">Inline view</a>
43+
</li>
4044

4145
<li class="divider"></li>
4246

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<p>The HTML template of a view below is inlined right in the top-level view.</p>
2+
3+
<div app-view-segment="2" class="well">
4+
<div class="">
5+
<h4>Slow data is loaded:</h4>
6+
<pre>{{data}}</pre>
7+
</div>
8+
</div>

src/view-segment.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,27 @@
8383
var locals = angular.extend({}, segment.locals),
8484
template = locals && locals.$template;
8585

86-
if (template) {
87-
88-
clearContent();
89-
90-
currentElement = tElement.clone();
91-
currentElement.html(template);
92-
animate.enter( currentElement, null, anchor );
93-
94-
var link = $compile(currentElement, false, 499), controller;
95-
96-
currentScope = $scope.$new();
97-
if (segment.params.controller) {
98-
locals.$scope = currentScope;
99-
controller = $controller(segment.params.controller, locals);
100-
if(segment.params.controllerAs)
101-
currentScope[segment.params.controllerAs] = controller;
102-
currentElement.data('$ngControllerController', controller);
103-
currentElement.children().data('$ngControllerController', controller);
104-
}
105-
106-
link(currentScope);
107-
currentScope.$emit('$viewContentLoaded');
108-
currentScope.$eval(onloadExp);
109-
} else {
110-
clearContent();
86+
clearContent();
87+
88+
currentElement = tElement.clone();
89+
currentElement.html(template ? template : defaultContent);
90+
animate.enter( currentElement, null, anchor );
91+
92+
var link = $compile(currentElement, false, 499), controller;
93+
94+
currentScope = $scope.$new();
95+
if (segment.params.controller) {
96+
locals.$scope = currentScope;
97+
controller = $controller(segment.params.controller, locals);
98+
if(segment.params.controllerAs)
99+
currentScope[segment.params.controllerAs] = controller;
100+
currentElement.data('$ngControllerController', controller);
101+
currentElement.children().data('$ngControllerController', controller);
111102
}
103+
104+
link(currentScope);
105+
currentScope.$emit('$viewContentLoaded');
106+
currentScope.$eval(onloadExp);
112107
}
113108
}
114109
}

test/unit/view-segment.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,66 @@ describe('view-segment', function() {
230230
$rootScope.$digest();
231231
expect(spy.calls.length).toBe(2);
232232
}))
233+
234+
describe('a view with empty template', function() {
235+
236+
beforeEach(function() {
237+
238+
$rootScope.foo = 'INIT';
239+
240+
$routeSegmentProvider.when('/3', 'section3');
241+
$routeSegmentProvider.when('/3/1/:id', 'section3.section31');
242+
$routeSegmentProvider.segment('section3', {
243+
template: '<div app:view-segment="1"><div>{{foo}}</div></div>'
244+
})
245+
$routeSegmentProvider.within('section3').segment('section31', {
246+
// no template
247+
controller: function($scope) {
248+
$scope.foo = 'CONTROLLER OVERRIDDEN '+$routeSegment.$routeParams.id;
249+
},
250+
dependencies: ['id']
251+
});
252+
})
253+
254+
it('should work with tag\'s content', function() {
255+
256+
$location.path('/3');
257+
258+
$rootScope.$digest();
259+
expect(elm.find('> div').text()).toBe('INIT');
260+
261+
$location.path('/3/1/1');
262+
263+
$rootScope.$digest();
264+
expect(elm.find('> div').text()).toBe('CONTROLLER OVERRIDDEN 1');
265+
266+
$location.path('/3/1/2');
267+
268+
$rootScope.$digest();
269+
expect(elm.find('> div').text()).toBe('CONTROLLER OVERRIDDEN 2');
270+
})
271+
272+
it('should reload', function() {
273+
274+
$location.path('/3/1/1');
275+
276+
$rootScope.$digest();
277+
expect(elm.find('> div').text()).toBe('CONTROLLER OVERRIDDEN 1');
278+
279+
elm.find('> div > div').scope().foo = 'DIRTY';
280+
281+
$rootScope.$digest();
282+
expect(elm.find('> div').text()).toBe('DIRTY');
283+
284+
$routeSegment.chain[1].reload();
285+
286+
$rootScope.$digest();
287+
expect(elm.find('> div').text()).toBe('CONTROLLER OVERRIDDEN 1');
288+
})
289+
290+
})
291+
292+
233293

234294

235295
});

0 commit comments

Comments
 (0)