Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Micro syntax `as` not working with my own directive

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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;
}
over 4 years ago · Santiago Trujillo Report

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 :(

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!