|
| 1 | +# Passing Values Nested |
| 2 | + |
| 3 | +## Statically |
| 4 | + |
| 5 | +```typescript |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'rxa-recursive-static', |
| 9 | + template: ` |
| 10 | + <ng-container *ngIf="level === 0; else: branch"> |
| 11 | + <p>Level {{total-level}}</p> |
| 12 | + <ng-container *embeddedViewLet="value$; let value">{{value}}<ng-container> |
| 13 | + </ng-container> |
| 14 | + <ng-template #branch> |
| 15 | + <p>Level {{total-level}}</p> |
| 16 | + <rxa-recursive-static [total]="total" [level]="level-1" [value]="value"></rxa-recursive-static> |
| 17 | + </ng-template> |
| 18 | + `, |
| 19 | + host: { |
| 20 | + class: 'd-flex w-100' |
| 21 | + }, |
| 22 | + providers: [CdHelper], |
| 23 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 24 | +}) |
| 25 | +export class RecursiveStaticComponent { |
| 26 | + |
| 27 | + @Input() |
| 28 | + set depth(d){ |
| 29 | + this.total = d; |
| 30 | + this.level = this.total -1; |
| 31 | + } |
| 32 | + |
| 33 | + @Input() |
| 34 | + total = 0; |
| 35 | + |
| 36 | + @Input() |
| 37 | + level = 0; |
| 38 | + |
| 39 | + @Input() |
| 40 | + value; |
| 41 | + |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Observable |
| 46 | + |
| 47 | +```typescript |
| 48 | +@Component({ |
| 49 | + selector: 'rxa-recursive-observable', |
| 50 | + template: ` |
| 51 | + <ng-container *ngIf="level === 0; else: branch"> |
| 52 | + <p>Level {{total - level}}</p> |
| 53 | + <ng-container *embeddedViewLet="value$; let value">{{value}}<ng-container> |
| 54 | + </ng-container> |
| 55 | + <ng-template #branch> |
| 56 | + <p>Level {{total - level}}</p> |
| 57 | + <rxa-recursive-observable [total]="total" [level]="level-1" [value$]="value$"></rxa-recursive-observable> |
| 58 | + </ng-template> |
| 59 | + `, |
| 60 | + host: { |
| 61 | + class: 'd-flex w-100' |
| 62 | + }, |
| 63 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 64 | +}) |
| 65 | +export class RecursiveObservableComponent { |
| 66 | + |
| 67 | + @Input() |
| 68 | + set depth(d) { |
| 69 | + this.total = d; |
| 70 | + this.level = this.total - 1; |
| 71 | + } |
| 72 | + |
| 73 | + @Input() |
| 74 | + total = 0; |
| 75 | + |
| 76 | + @Input() |
| 77 | + level = 0; |
| 78 | + |
| 79 | + @Input() value$: Observable<any>; |
| 80 | + |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Async Pipe |
| 85 | + |
| 86 | +```typescript |
| 87 | +@Component({ |
| 88 | + selector: 'rxa-recursive-async', |
| 89 | + template: ` |
| 90 | + <ng-container *ngIf="level === 0; else: branch"> |
| 91 | + <p>Level {{total-level}}</p> |
| 92 | + {{value$ | async}} |
| 93 | + </ng-container> |
| 94 | + <ng-template #branch> |
| 95 | + <p>Level {{total-level}}</p> |
| 96 | + <rxa-recursive-async [total]="total" [level]="level-1" [value]="value$ | async"></rxa-recursive-async> |
| 97 | + </ng-template> |
| 98 | + `, |
| 99 | + host: { |
| 100 | + class: 'd-flex w-100' |
| 101 | + }, |
| 102 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 103 | +}) |
| 104 | +export class RecursiveAsyncComponent { |
| 105 | + @Input() |
| 106 | + set depth(d){ |
| 107 | + this.total = d; |
| 108 | + this.level = this.total -1; |
| 109 | + } |
| 110 | + |
| 111 | + @Input() |
| 112 | + total = 0; |
| 113 | + |
| 114 | + @Input() |
| 115 | + level = 0; |
| 116 | + |
| 117 | + value$ = new Subject(); |
| 118 | + @Input() |
| 119 | + set value(v) { |
| 120 | + this.value$.next(v) |
| 121 | + }; |
| 122 | + |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Push Pipe |
| 127 | + |
| 128 | +```typescript |
| 129 | +@Component({ |
| 130 | + selector: 'rxa-recursive-push', |
| 131 | + template: ` |
| 132 | + <ng-container *ngIf="level === 0; else: branch"> |
| 133 | + <p>Level {{total-level}}</p> |
| 134 | + {{value$ | push}} |
| 135 | + </ng-container> |
| 136 | + <ng-template #branch> |
| 137 | + <p>Level {{total-level}}</p> |
| 138 | + <rxa-recursive-push [total]="total" [level]="level-1" [value]="value$ | push"></rxa-recursive-push> |
| 139 | + </ng-template> |
| 140 | + `, |
| 141 | + host: { |
| 142 | + class: 'd-flex w-100' |
| 143 | + }, |
| 144 | + providers: [CdHelper], |
| 145 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 146 | +}) |
| 147 | +export class RecursivePushComponent { |
| 148 | + @Input() |
| 149 | + set depth(d){ |
| 150 | + this.total = d; |
| 151 | + this.level = this.total -1; |
| 152 | + } |
| 153 | + |
| 154 | + @Input() |
| 155 | + total = 0; |
| 156 | + |
| 157 | + @Input() |
| 158 | + level = 0; |
| 159 | + |
| 160 | + value$ = new Subject(); |
| 161 | + @Input() |
| 162 | + set value(v) { |
| 163 | + this.value$.next(v) |
| 164 | + }; |
| 165 | + |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## LetDirective - Component based Change Detection |
| 170 | + |
| 171 | +```typescript |
| 172 | +@Component({ |
| 173 | + selector: 'rxa-recursive-component-let', |
| 174 | + template: ` |
| 175 | + <ng-container *ngIf="level === 0; else: branch"> |
| 176 | + <p>Level {{total-level}}</p> |
| 177 | + <rxa-renders *componentLet="value$; let v" [value$]="v"></rxa-renders> |
| 178 | + </ng-container> |
| 179 | + <ng-template #branch> |
| 180 | + <p>Level {{total-level}}</p> |
| 181 | + <rxa-recursive-component-let [total]="total" *componentLet="value$; let v" [level]="level-1" [value]="v"></rxa-recursive-component-let> |
| 182 | + </ng-template> |
| 183 | + `, |
| 184 | + host: { |
| 185 | + class: 'd-flex w-100' |
| 186 | + }, |
| 187 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 188 | +}) |
| 189 | +export class RecursiveComponentLetComponent { |
| 190 | + @Input() |
| 191 | + set depth(d){ |
| 192 | + this.total = d; |
| 193 | + this.level = this.total -1; |
| 194 | + } |
| 195 | + |
| 196 | + @Input() |
| 197 | + total = 0; |
| 198 | + |
| 199 | + @Input() |
| 200 | + level = 0; |
| 201 | + |
| 202 | + value$ = new ReplaySubject(1) |
| 203 | + @Input() |
| 204 | + set value(v) { |
| 205 | + this.value$.next(v) |
| 206 | + }; |
| 207 | + |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +## LetDirective - EmbeddedView based Change Detection |
| 212 | + |
| 213 | +```typescript |
| 214 | +@Component({ |
| 215 | + selector: 'rxa-recursive-embedded-view-let', |
| 216 | + template: ` |
| 217 | + <ng-container *ngIf="level === 0; else: branch"> |
| 218 | + <p>Level {{total-level}}</p> |
| 219 | + <ng-container *componentLet="value$; let value">{{value}}</ng-container> |
| 220 | + </ng-container> |
| 221 | + <ng-template #branch> |
| 222 | + <p>Level {{total-level}}</p> |
| 223 | + <rxa-recursive-embedded-view-let [total]="total" *componentLet="value$; let v" [level]="level-1" [value]="v"></rxa-recursive-embedded-view-let> |
| 224 | + </ng-template> |
| 225 | + `, |
| 226 | + host: { |
| 227 | + class: 'd-flex w-100' |
| 228 | + }, |
| 229 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 230 | +}) |
| 231 | +export class RecursiveEmbeddedViewLetComponent { |
| 232 | + @Input() |
| 233 | + set depth(d){ |
| 234 | + this.total = d; |
| 235 | + this.level = this.total -1; |
| 236 | + } |
| 237 | + |
| 238 | + @Input() |
| 239 | + total = 0; |
| 240 | + |
| 241 | + @Input() |
| 242 | + level = 0; |
| 243 | + |
| 244 | + value$ = new ReplaySubject(1) |
| 245 | + @Input() |
| 246 | + set value(v) { |
| 247 | + this.value$.next(v) |
| 248 | + }; |
| 249 | + |
| 250 | +} |
| 251 | +``` |
| 252 | + |
0 commit comments