question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(compiler): Add ngProjectAs to Directive and Component metadata

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

compiler

Description

Presently we can specify ngProjectAs on an element to trick ViewChild and <ng-container> into picking it up.

Example:

<mat-form-field> <my-bundle-o-errors ngProjectAs="mat-error"> </my-bundle-o-errors> </mat-form-field>

However, this requires correctly repeating ngProjectAs=“mat-error” on every invocation of that component.

Proposed solution

By adding ngProjectAs to Directive, we can allow the renamed projection to work for every instance of a component or directive.

The behavior would be the same as when it appears on an element (effectively setting ngProjectAs on the host element at compile time).

Alternatives considered

If desired, this could be limited to Component rather than Directive metadata.

The code snipped above can be augmented with runtime assertions that ensure ngProjectAs is present:

@Input() ngProjectAs?: string;

ngOnInit() {
  if (this.ngProjectAs !== 'mat-error') throw new Error('ngProjectAs="mat-error" is required');
}

Still not as nice as being able to include it in the component’s own definition.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kseamoncommented, Nov 17, 2021

The behavior could be additive - projection could match both the actual as well as the “projected as” signature.

Often times, the projecting component is not owned by the same entity as the new directive/component being implemented. In my case, the motive for this is being able to use the mat-error slot in mat-form-field.

0reactions
angular-automatic-lock-bot[bot]commented, Jul 7, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Directive - Angular
When present, this directive/component is ignored by the AOT compiler. ... The options provide configuration metadata that determines how the directive ...
Read more >
Need ability to add directives to host elements in component ...
I've been digging into Angular 2 and have run into a potential road block for extending certain kinds of components. In the the...
Read more >
angular - When is a directive required in component metadata ...
A component needs to define in its directives the Components/Directive which are referenced within its template via their selectors.
Read more >
Angular Directive Tutorial With Example | Custom Directives
This blog will introduce you to Angular Directive with sample codes for better understanding. You will also learn about Custom Directives in ...
Read more >
Difference between Angular directives vs components - eduCBA
To make the newly created Angular Component a member of NgModule, we should be adding this component name in the declarations field of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found