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.

Add an option in the @Component() decorator to add scope to the view template

See original GitHub issue

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

common

Description

To expose constants or enums to a template is messy and becomes uncomfortable to manage.

It would be great if there was another way to bind data without adding it to the class.

Proposed solution

Add an option to the @Component() decorator to quickly bind data without polluting the class.

It could look like this:

import { myBigData, MY_CONSTANT } from './data';

enum Colours {
  Red,
  Green,
  Blue,
}

@Component({
  selector: 'app-colours',
  templateUrl: './colours.html',
  scope: [Colours, myBigData, MY_CONSTANT],
})
class ColoursComponent {}

These would be available in the template.

<button (click)="trigger(Colours.red)"></button>

<div *ngFor="let item of myBigData">{{ item }}</div>

{{ MY_CONSTANT }}

Alternatives considered

An alternative could be a second decorator potentially, however that doesn’t sound great. A different name could work too of course.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:19
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Jul 30, 2021

Thanks for linking the duplicates @rafaelss95; closing as dup of #25963.

0reactions
angular-automatic-lock-bot[bot]commented, Aug 30, 2021

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

Component - Angular
Decorator that marks a class as an Angular component and provides configuration metadata that determines how the component should be processed, instantiated ...
Read more >
Angular @ViewChild: In-Depth Explanation (All Features ...
The @ViewChild decorator cannot see across component boundaries! Visibility scope of @ViewChild template queries. This means that queries done ...
Read more >
Understanding the ViewChild and ViewChildren decorators in ...
A view query is a requested reference to a child element within a component view which contains metadata of the element. The scope...
Read more >
Templates, Styles & View Encapsulation • Angular - codecraft.tv
We've covered the basics of the @Component decorator in the quickstart. We explained how decorators work and both the template and selector configuration ......
Read more >
A deep dive on Angular decorators - Ultimate Courses
With decorators, we can simply put the @Input() decorator above the ... selector: 'example-component', template: '<div>Woo a component!
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