@@ -11,6 +11,7 @@ import {
11
11
Injector ,
12
12
ApplicationRef ,
13
13
} from '@angular/core' ;
14
+ import { CommonModule } from '@angular/common' ;
14
15
import { TemplatePortalDirective , PortalHostDirective , PortalModule } from './portal-directives' ;
15
16
import { Portal , ComponentPortal } from './portal' ;
16
17
import { DomPortalHost } from './dom-portal-host' ;
@@ -123,6 +124,28 @@ describe('Portals', () => {
123
124
expect ( hostContainer . textContent ) . toContain ( 'Mango' ) ;
124
125
} ) ;
125
126
127
+ it ( 'should load a <ng-template> portal with an inner template' , ( ) => {
128
+ let testAppComponent = fixture . debugElement . componentInstance ;
129
+
130
+ // Detect changes initially so that the component's ViewChildren are resolved.
131
+ fixture . detectChanges ( ) ;
132
+
133
+ // Set the selectedHost to be a TemplatePortal.
134
+ testAppComponent . selectedPortal = testAppComponent . portalWithTemplate ;
135
+ fixture . detectChanges ( ) ;
136
+
137
+ // Expect that the content of the attached portal is present.
138
+ let hostContainer = fixture . nativeElement . querySelector ( '.portal-container' ) ;
139
+ expect ( hostContainer . textContent ) . toContain ( 'Pineapple' ) ;
140
+
141
+ // When updating the binding value.
142
+ testAppComponent . fruits = [ 'Mangosteen' ] ;
143
+ fixture . detectChanges ( ) ;
144
+
145
+ // Expect the new value to be reflected in the rendered output.
146
+ expect ( hostContainer . textContent ) . toContain ( 'Mangosteen' ) ;
147
+ } ) ;
148
+
126
149
it ( 'should change the attached portal' , ( ) => {
127
150
let testAppComponent = fixture . debugElement . componentInstance ;
128
151
@@ -258,6 +281,15 @@ describe('Portals', () => {
258
281
expect ( someDomElement . textContent ) . toContain ( 'Cake' ) ;
259
282
} ) ;
260
283
284
+ it ( 'should render a template portal with an inner template' , ( ) => {
285
+ let fixture = TestBed . createComponent ( PortalTestApp ) ;
286
+ fixture . detectChanges ( ) ;
287
+
288
+ fixture . componentInstance . portalWithTemplate . attach ( host ) ;
289
+
290
+ expect ( someDomElement . textContent ) . toContain ( 'Durian' ) ;
291
+ } ) ;
292
+
261
293
it ( 'should attach and detach a template portal with a binding' , ( ) => {
262
294
let fixture = TestBed . createComponent ( PortalTestApp ) ;
263
295
@@ -384,14 +416,21 @@ class ArbitraryViewContainerRefComponent {
384
416
<ng-template cdk-portal>Cake</ng-template>
385
417
386
418
<div *cdk-portal>Pie</div>
387
-
388
- <ng-template cdk-portal> {{fruit}} </ng-template>` ,
419
+ <ng-template cdk-portal> {{fruit}} </ng-template>
420
+
421
+ <ng-template cdk-portal>
422
+ <ul>
423
+ <li *ngFor="let fruitName of fruits"> {{fruitName}} </li>
424
+ </ul>
425
+ </ng-template>
426
+ ` ,
389
427
} )
390
428
class PortalTestApp {
391
429
@ViewChildren ( TemplatePortalDirective ) portals : QueryList < TemplatePortalDirective > ;
392
430
@ViewChild ( PortalHostDirective ) portalHost : PortalHostDirective ;
393
431
selectedPortal : Portal < any > ;
394
432
fruit : string = 'Banana' ;
433
+ fruits = [ 'Apple' , 'Pineapple' , 'Durian' ] ;
395
434
396
435
constructor ( public injector : Injector ) { }
397
436
@@ -406,13 +445,17 @@ class PortalTestApp {
406
445
get portalWithBinding ( ) {
407
446
return this . portals . toArray ( ) [ 2 ] ;
408
447
}
448
+
449
+ get portalWithTemplate ( ) {
450
+ return this . portals . toArray ( ) [ 3 ] ;
451
+ }
409
452
}
410
453
411
454
// Create a real (non-test) NgModule as a workaround for
412
455
// https://github.com/angular/angular/issues/10760
413
456
const TEST_COMPONENTS = [ PortalTestApp , ArbitraryViewContainerRefComponent , PizzaMsg ] ;
414
457
@NgModule ( {
415
- imports : [ PortalModule ] ,
458
+ imports : [ CommonModule , PortalModule ] ,
416
459
exports : TEST_COMPONENTS ,
417
460
declarations : TEST_COMPONENTS ,
418
461
entryComponents : TEST_COMPONENTS ,
0 commit comments