Access modifiers control who can use or change parts of a component. They help keep your code safe and organized.
Access modifiers in components in Angular
class MyComponent { public name: string; // accessible everywhere private secret: string; // accessible only inside this class protected age: number; // accessible inside this class and subclasses }
public means anyone can access it.
private means only the component itself can access it.
protected means the component and its child classes can access it.
export class UserComponent { public username = 'Alice'; private password = '1234'; protected role = 'admin'; }
export class ProductComponent { private price = 100; public getPrice() { return this.price; } }
This Angular component shows how public and private access modifiers work. The template can show publicInfo directly but cannot access secretInfo directly. Instead, it uses a public method getSecret() to get the private data.
import { Component } from '@angular/core'; @Component({ selector: 'app-sample', template: `<p>{{ publicInfo }}</p><p>{{ getSecret() }}</p>` }) export class SampleComponent { public publicInfo = 'Visible to everyone'; private secretInfo = 'Hidden from template'; public getSecret() { return this.secretInfo; } }
Templates can only access public members of the component.
Use private to hide details that should not be changed outside the component.
protected is useful when you extend components and want child classes to access some members.
Access modifiers control visibility and safety of component parts.
public is open to all, private is hidden inside the component, and protected is for the component and its children.
Templates can only use public members directly.
Practice
Solution
Step 1: Understand template access rules
Angular templates can only access properties and methods marked aspublic.Step 2: Compare access modifiers
privateandprotectedmembers are not accessible in templates, onlypublicmembers are.Final Answer:
public -> Option DQuick Check:
Templates access public members only [OK]
- Thinking private members are accessible in templates
- Confusing protected with public for template access
- Assuming readonly affects visibility
count in an Angular component?Solution
Step 1: Recall TypeScript property declaration syntax
Access modifier comes first, then property name, colon, and type.Step 2: Check each option
private count: number; matches correct syntax:private count: number;. public count: number; is public, not private. protected count = number; has wrong assignment syntax. count private: number; has wrong order.Final Answer:
private count: number; -> Option BQuick Check:
Access modifier + name + colon + type [OK]
- Placing access modifier after property name
- Using assignment instead of type annotation
- Mixing up protected and private keywords
export class MyComponent {
private secret = 'hidden';
public visible = 'shown';
}
Which property can be accessed in the component's template?Solution
Step 1: Identify access modifiers of properties
secretis private,visibleis public.Step 2: Recall template access rules
Templates can only access public members, so onlyvisibleis accessible.Final Answer:
visible -> Option CQuick Check:
Only public properties are template-accessible [OK]
- Assuming private properties are accessible in templates
- Thinking both properties are accessible
- Confusing private with protected
export class DemoComponent {
private title = 'Hello';
}
// In template:
// <h1>{{ title }}</h1>Solution
Step 1: Check property access modifier
titleis declared private, so it is hidden from outside the class.Step 2: Understand template access rules
Angular templates can only access public properties, so trying to usetitlein template causes an error.Final Answer:
Template cannot access private property 'title' -> Option AQuick Check:
Private properties are not accessible in templates [OK]
- Ignoring access modifier effect on templates
- Thinking missing semicolon causes error
- Confusing protected with private for template access
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?Solution
Step 1: Understand access modifier meanings
privatehides from all except the class itself,publicexposes everywhere including templates,protectedallows access in the class and its subclasses but not templates.Step 2: Match requirement to modifier
Sincedatashould be accessible in child components but hidden from templates,protectedfits perfectly.Final Answer:
protected -> Option AQuick Check:
Protected = accessible in children, hidden from templates [OK]
- Choosing public and exposing property to templates
- Using private and blocking child access
- Confusing readonly with access control
Ready to prove it?
Pass the test and this pattern moves unknown
