1

I want to support the following micro syntax:

foo$ | async as xyz

Example:

<p *appVar="foo$ | async as xyz">
   Value is {{ xyz }}!
</p>

That stream is defined as

$foo = of('some value')

Here is my directive:

@Directive({
   selector: '[appVar]'
})
export class VarDirective<T> {
   @Input() appVar: T;

   constructor(
      private templateRef: TemplateRef<T>,
      private viewContainer: ViewContainerRef
   ) {}

   ngOnInit(): void {
      this.viewContainer.createEmbeddedView(this.templateRef);
   }
}

DEMO

Now, whatever I do, that xyz variable is always undefined.

If I create a context (which is suggested in this post)

 const context = { $implicit: this.appVar };
 this.viewContainer.createEmbeddedView(this.templateRef, context);

DEMO

I get the same result. Any suggestions what I do wrong here?

skink
  • 5,133
  • 6
  • 37
  • 58
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

2 Answers2

3

The expression has to be available in the context under the same name as the directive itself, which is appVar in this case:

this.viewContainer.createEmbeddedView(this.templateRef, {
  $implicit: this.appVar,
  appVar: this.appVar
});

Demo

Compare it to the built-in ngIf directive:

@Input()
set ngIf(condition: any) {
  this._context.$implicit = this._context.ngIf = condition;
  this._updateView();
}

and to the TuiLetContext:

get $implicit(): T {
  return this.internalDirectiveInstance.tuiLet;
}

get tuiLet(): T {
  return this.internalDirectiveInstance.tuiLet;
}
skink
  • 5,133
  • 6
  • 37
  • 58
0

The only way I know is using "let" not "as". Some like

@Input() appVar

ngOnInit(): void {
    this.viewContainer.createEmbeddedView(this.templateRef,
                {$implicit:this.appVar});
}

And use

<p *appVar="foo$ | async;let data">
  Value is {{ data}}!
</p>

But I feel that it's not what are you looking for :(

Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • I'm pretty sure it should be possible. For example TuiLet ([article](https://medium.com/its-tinkoff/how-taiga-ui-cdk-can-help-you-simplify-your-working-with-angular-our-5-best-practices-fd44809a8e2d)) does exactly the same thing. TuiLet [source code](https://github.com/TinkoffCreditSystems/taiga-ui/blob/main/projects/cdk/directives/let/let.directive.ts) file – Jeanluca Scaljeri Sep 10 '21 at 09:59