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.

ng-container: need warning when ng-template variable matches component variable

See original GitHub issue

πŸš€ feature request

Relevant Package

@angular/core, I think. Wherever the structural directives live.

Description

If a component variable and an ng-template variable use the same name and are used within the same ng-template section, then the ng-template variable will always be used. We need to indicate this behavior to the user.

Describe the solution you’d like

It would be great if we could get a warning in this situation.

Describe alternatives you’ve considered

Using unique names for the variables resolves the situation.

See this Stackblitz example. The important code is as follows:

@Component({`
  selector: 'hello',
  template: `
  <ng-container> *ngIf='name;then name else donthandlename' </ng-container>
  <ng-template #name> Hello {{ name }} </ng-template>
  <ng-template #donthandlename> Hello unknown person </ng-template>
  `,
  styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent  {
  @Input() name: string;
}

The basic idea is to use one template if the class’s input variable β€˜name’ has been given to the component, and to use a different template if β€˜name’ is not defined. When we get to β€˜Hello {{ name }}’, the definition of β€˜name’ seems ambiguous: it could refer to the component’s input variable, or it could refer to the TemplateRef being used in the enclosing template:

<ng-template #name>

As it turns out, Angular uses the TemplateRef from ng-template, not the input variable. By itself, that isn’t necessarily broken, but it would be great if β€˜name’ in β€˜Hello {{ name }}’ was underlined in red in order to highlight the ambiguity. (I’m using Visual Studio Code, by the way.)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesHenrycommented, Jun 4, 2021

Any linting capabilities in this domain would have to be built on top of the angular compiler for this very reason

0reactions
angular-automatic-lock-bot[bot]commented, Jun 27, 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

Angular ng-template, ng-container and ngTemplateOutlet
This is a Complete Guide To Angular Templates: it covers ng-template, ng-container, the ngTemplateOutlet structural directive, and more.
Read more >
How to declare a variable in a template in Angular
You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a...
Read more >
Template type checking - Angular
Verifies that component/directive bindings are assignable to their @Input() s; Obeys TypeScript's strictNullChecks flag when validating the preceding modeΒ ...
Read more >
Use <ng-template>. Part 6 of Advanced Angular Component…
dev. Thanks for being part of indepth movement! β€Ή 05 Handle Template Reference Variables with Directives Β· 07 Use Content Directives β€Ί Β·...
Read more >
Angular Templates β€” From Start to Source | Unicorn Utterances
This template reference variable makes falseTemp a valid variable to use ... to grab a reference to the ng-template from the component logicΒ ......
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