Performance: Access modifiers in components
Access modifiers affect how component properties and methods are exposed, impacting template binding and change detection performance.
export class MyComponent { private data = []; updateData() { // update logic } }
export class MyComponent { public data = []; public updateData() { // update logic } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Public properties bound in template | High (many bindings) | Medium (due to frequent updates) | Medium | [X] Bad |
| Private properties not bound in template | Low (fewer bindings) | Low | Low | [OK] Good |
public.private and protected members are not accessible in templates, only public members are.count in an Angular component?private count: number;. public count: number; is public, not private. protected count = number; has wrong assignment syntax. count private: number; has wrong order.export class MyComponent {
private secret = 'hidden';
public visible = 'shown';
}
Which property can be accessed in the component's template?secret is private, visible is public.visible is accessible.export class DemoComponent {
private title = 'Hello';
}
// In template:
// <h1>{{ title }}</h1>title is declared private, so it is hidden from outside the class.title in template causes an error.data in a base Angular component to be accessible in its child components but not directly in the template. Which access modifier should you use?private hides from all except the class itself, public exposes everywhere including templates, protected allows access in the class and its subclasses but not templates.data should be accessible in child components but hidden from templates, protected fits perfectly.Pass the test and this pattern moves unknown