Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 301cc9b

Browse files
feat(ng-view): support multiple ng-view on the same page and nested ng-view
1 parent ea57939 commit 301cc9b

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

src/directive/ngView.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
* configuration of the `$route` service.
1414
*
1515
* @scope
16+
*
17+
* @param {string=} template Specify the name of the property in the route object containing the source of
18+
* the template to load. By default, ng-view will look for a property named 'template'
19+
* in the current route. Use this property to use more than one ng-view on the same page.
20+
*
1621
* @example
1722
<doc:example module="ngView">
1823
<doc:source>
@@ -113,15 +118,16 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
113118
link: function(scope, element, attr) {
114119
var changeCounter = 0,
115120
lastScope,
116-
onloadExp = attr.onload || '';
121+
onloadExp = attr.onload || '',
122+
templateProp = attr.template || 'template';
117123

118124
scope.$on('$afterRouteChange', function(event, next, previous) {
119125
changeCounter++;
120126
});
121127

122128
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
123-
var template = $route.current && $route.current.template;
124-
129+
var template = $route.current && $route.current[templateProp];
130+
125131
function destroyLastScope() {
126132
if (lastScope) {
127133
lastScope.$destroy();

test/directive/ngViewSpec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,95 @@ describe('ng-view', function() {
411411
});
412412
})
413413
});
414+
415+
describe('ng-view with configurable template', function() {
416+
var element;
417+
418+
beforeEach(module(function() {
419+
return function($rootScope, $compile) {
420+
element = $compile('<ng:view template="template2"></ng:view>')($rootScope);
421+
};
422+
}));
423+
424+
afterEach(function(){
425+
dealoc(element);
426+
});
427+
428+
it('should use the configured template', function() {
429+
module(function($routeProvider) {
430+
$routeProvider.when('/route1234', {template: 'myUrl1', template2: 'myUrl2'});
431+
});
432+
433+
inject(function($rootScope, $compile, $httpBackend, $location, $route) {
434+
expect(element.text()).toEqual('');
435+
436+
$location.path('/route1234');
437+
$httpBackend.expect('GET', 'myUrl2').respond('<div>{{1+3}}</div>');
438+
$rootScope.$digest();
439+
$httpBackend.flush();
440+
expect(element.text()).toEqual('4');
441+
});
442+
});
443+
});
444+
445+
describe('ng-view twice on the same page', function() {
446+
var element;
447+
448+
beforeEach(module(function() {
449+
return function($rootScope, $compile) {
450+
element = $compile('<div><div id="view1"><ng:view template="t1"></ng:view></div>_<div id="view2"><ng:view template="t2"></ng:view></div></div>')($rootScope);
451+
};
452+
}));
453+
454+
afterEach(function(){
455+
dealoc(element);
456+
});
457+
458+
it('should render both views', function() {
459+
module(function($routeProvider) {
460+
$routeProvider.when('/route1234', {t1: 'page1', t2: 'page2'});
461+
});
462+
463+
inject(function($rootScope, $compile, $httpBackend, $location, $route) {
464+
expect(element.text()).toEqual('_');
465+
466+
$location.path('/route1234');
467+
$httpBackend.expect('GET', 'page1').respond('<div>{{10+1}}</div>');
468+
$httpBackend.expect('GET', 'page2').respond('<div>{{10+2}}</div>');
469+
$rootScope.$digest();
470+
$httpBackend.flush();
471+
expect(element.text()).toEqual('11_12');
472+
});
473+
});
474+
});
475+
476+
describe('ng-view nested', function() {
477+
var element;
478+
479+
beforeEach(module(function() {
480+
return function($rootScope, $compile) {
481+
element = $compile('<ng:view template="layout"></ng:view>')($rootScope);
482+
};
483+
}));
484+
485+
afterEach(function(){
486+
dealoc(element);
487+
});
488+
489+
it('should render nested views', function() {
490+
module(function($routeProvider) {
491+
$routeProvider.when('/route1234', {layout: 'layout1', content: 'content1'});
492+
});
493+
494+
inject(function($rootScope, $compile, $httpBackend, $location, $route) {
495+
expect(element.text()).toEqual('');
496+
497+
$location.path('/route1234');
498+
$httpBackend.expect('GET', 'layout1').respond('layout:<ng-view template="content"></ng-view>');
499+
$httpBackend.expect('GET', 'content1').respond('content');
500+
$rootScope.$digest();
501+
$httpBackend.flush();
502+
expect(element.text()).toEqual('layout:content');
503+
});
504+
});
505+
});

0 commit comments

Comments
 (0)