Using ngOnInit for Initialization in Angular
📖 Scenario: You are building a simple Angular component that shows a welcome message and a list of favorite fruits. You want to set up the initial data when the component starts.
🎯 Goal: Create an Angular standalone component that initializes a message string and a fruits array inside the ngOnInit lifecycle method. Display the message and the list of fruits in the template.
📋 What You'll Learn
Create a standalone Angular component named
WelcomeComponentInitialize a
message string and a fruits array inside ngOnInitDisplay the
message in an <h1> tagDisplay the
fruits array items in an unordered list <ul>Use Angular's
OnInit interface and implement ngOnInit method💡 Why This Matters
🌍 Real World
Components often need to set up data when they start. Using ngOnInit is the standard way to initialize data in Angular components.
💼 Career
Understanding Angular lifecycle hooks like ngOnInit is essential for building maintainable and predictable Angular applications in professional development.
Progress0 / 4 steps