[Feature request] Compile time expressions in template
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:
Current behavior
As i can see angular template syntax is not providing a way to declare a constant compile time expressions in template. Any {{ expression }}
should be invoked in runtime in order to be tracked for changing. But it might be useful to declare an expression which won’t be changed, but still referenc it from component’s class.
What is the motivation / use case for changing the behavior?
Let me show you example of use case.
export { globalConstMessages } from './utils/messages'
@Component({
selector: 'app',
template: '<p>{{ text }}</p>' // or may be some other syntax something like <p>@text</p>
})
export class MyComponent {
public readonly text: string = globalConstMessages.someText; // <-- 'Hello world' for example
}
There is no way to transform this template at compile-time into
@Component({
selector: 'app',
template: '<p>Hello world</p>'
})
export class MyComponent {
// empty here, no variable at all
}
In this case we will be able to avoid unnessesary change detections in run-time. It will be usefull in cases where we need to reuse some text messages across application, and it will be much easier to maintain this messages in one file and then only reference them as constant compile-time expression in other templates.
Also it will be a nice feature if we will be able to declare an @Input’s variable of other component’s as a constant compile-time expressions, but i think it will be much harder to implement.
For example
@Component({
selector: 'tooltip',
template: '<p>{{ text}}</p>'
})
export class TooltipComponent {
@Input('text')
public text: string;
}
@Component({
selector: 'app',
template: '<tooltip [text]="constantText"></tooltip>'
})
export class MyComponent {
public readonly constantText: string = 'Hello world!';
}
and it should be transformed at compile-time into
@Component({
selector: 'app',
template: '<p>Hello world</p>'
})
export class MyComponent {}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:37 (17 by maintainers)
@trotyl Yea, and that’s it my feature request was about. Let angular template compiler be more intelligent, provide constant folding, let it to optimize constant expression and inline them (of course with some help from user, such as decorators etc).
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.