6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { AfterContentInit , Attribute , ContentChildren , Directive , Input , QueryList , TemplateRef , ViewContainerRef } from '@angular/core' ;
9
+ import { Attribute , Directive , Host , Input , OnInit , TemplateRef , ViewContainerRef } from '@angular/core' ;
10
+
10
11
import { isPresent } from '../facade/lang' ;
11
12
import { NgLocalization , getPluralCategory } from '../localization' ;
13
+
12
14
import { SwitchView } from './ng_switch' ;
13
15
16
+
14
17
/**
15
18
* `ngPlural` is an i18n directive that displays DOM sub-trees that match the switch expression
16
19
* value, or failing that, DOM sub-trees that match the switch expression's pluralization category.
@@ -64,27 +67,11 @@ import {SwitchView} from './ng_switch';
64
67
* ```
65
68
* @experimental
66
69
*/
67
-
68
- @Directive ( { selector : '[ngPluralCase]' } )
69
- export class NgPluralCase {
70
- /** @internal */
71
- _view : SwitchView ;
72
- constructor (
73
- @Attribute ( 'ngPluralCase' ) public value : string , template : TemplateRef < Object > ,
74
- viewContainer : ViewContainerRef ) {
75
- this . _view = new SwitchView ( viewContainer , template ) ;
76
- }
77
- }
78
-
79
- /**
80
- * @experimental
81
- */
82
70
@Directive ( { selector : '[ngPlural]' } )
83
- export class NgPlural implements AfterContentInit {
71
+ export class NgPlural {
84
72
private _switchValue : number ;
85
73
private _activeView : SwitchView ;
86
74
private _caseViews : { [ k : string ] : SwitchView } = { } ;
87
- @ContentChildren ( NgPluralCase ) cases : QueryList < NgPluralCase > = null ;
88
75
89
76
constructor ( private _localization : NgLocalization ) { }
90
77
@@ -94,12 +81,7 @@ export class NgPlural implements AfterContentInit {
94
81
this . _updateView ( ) ;
95
82
}
96
83
97
- ngAfterContentInit ( ) {
98
- this . cases . forEach ( ( pluralCase : NgPluralCase ) : void => {
99
- this . _caseViews [ pluralCase . value ] = pluralCase . _view ;
100
- } ) ;
101
- this . _updateView ( ) ;
102
- }
84
+ addCase ( value : string , switchView : SwitchView ) : void { this . _caseViews [ value ] = switchView ; }
103
85
104
86
/** @internal */
105
87
_updateView ( ) : void {
@@ -122,3 +104,15 @@ export class NgPlural implements AfterContentInit {
122
104
this . _activeView . create ( ) ;
123
105
}
124
106
}
107
+
108
+ /**
109
+ * @experimental
110
+ */
111
+ @Directive ( { selector : '[ngPluralCase]' } )
112
+ export class NgPluralCase {
113
+ constructor (
114
+ @Attribute ( 'ngPluralCase' ) public value : string , template : TemplateRef < Object > ,
115
+ viewContainer : ViewContainerRef , @Host ( ) ngPlural : NgPlural ) {
116
+ ngPlural . addCase ( value , new SwitchView ( viewContainer , template ) ) ;
117
+ }
118
+ }
0 commit comments