Bird
0
0

In an Angular component, you have a private property _count and want to expose it safely to the template without allowing direct modification. Which pattern is best?

hard📝 component behavior Q9 of 15
Angular - TypeScript in Angular
In an Angular component, you have a private property _count and want to expose it safely to the template without allowing direct modification. Which pattern is best?
AMake _count public
BUse a public setter method for _count
CUse a public getter method for _count
DMake _count protected
Step-by-Step Solution
Solution:
  1. Step 1: Understand encapsulation

    Private property should not be exposed directly to prevent modification.
  2. Step 2: Use public getter

    A public getter allows read-only access in template safely.
  3. Final Answer:

    Use a public getter method for _count -> Option C
  4. Quick Check:

    Getter exposes private safely = best practice [OK]
Quick Trick: Use public getter to expose private properties safely [OK]
Common Mistakes:
  • Making private property public directly
  • Using setter instead of getter
  • Making property protected expecting template access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes