Tuesday, March 4, 2025

Angular Intermediate?

Template-driven forms:

  • Approach: Forms are built using Angular directives and standard HTML form elements.

  • Control Management: Form control directives (ngModel, ngForm, etc.) are used in the template to manage the form.

  • Binding: Two-way data binding ([(ngModel)]) is heavily used, making it easy to synchronize the data between the form and the model.

  • Validation: Validation is set up using Angular’s built-in directives (required, minlength, etc.) directly in the template.

  • Simplicity: Ideal for simple, straightforward forms as the setup is less complex and requires less boilerplate code.

  • Code Location: Most of the form-related logic resides in the template.

Reactive forms:

  • Approach: Forms are built programmatically in the component class, giving more control over the form structure and validation.

  • Control Management: Form controls are created and managed using FormControl, FormGroup, and FormArray in the component class.

  • Binding: Data binding is explicit and unidirectional, with the form model explicitly synced to the form controls.

  • Validation: Validators are applied directly to the form controls in the component class, providing more flexibility and custom validation.

  • Scalability: Suitable for more complex forms, as it offers better testability, scalability, and predictable behavior.

  • Code Location: Most of the form-related logic resides in the component class.

To summarize:

  • Template-driven forms are simpler and more declarative, best for straightforward use cases.

  • Reactive forms provide greater control, flexibility, and robustness, making them suitable for complex forms.

Lifecycle hooks in Angular are special

In Angular, lifecycle hooks are special methods that get called at specific points in a component's lifecycle, like when it's created, updated, or destroyed. 

Some key Angular lifecycle hooks include: ngOnInit (runs after the component is initialized), ngOnChanges (responds to input changes), ngDoCheck (detects changes that Angular can't), and ngOnDestroy (cleans up before the component is destroyed).


Angular Routing lets you define routes in a configuration object, mapping paths to components. When a user navigates to a URL, Angular Router activates the corresponding component, updating the view with new content. You can also pass parameters and handle navigation events.
 
Lazy loading in Angular is a technique where feature modules are loaded only when they're needed, rather than upfront. This improves application load time by splitting the app into chunks and loading them as the user navigates through the app.

In Angular, the @Injectable decorator is used to mark a class as available for dependency injection. It tells Angular that the class can be injected into other components or services.

Observables in Angular are a way to handle asynchronous data. They allow you to subscribe to a data stream and react to changes over time. Think of them as a more powerful and flexible version of promises, dealing with multiple values.
Promises, on the other hand, deal with a single value or error and are simpler but less flexible. Observables offer more powerful functionality for complex scenarios.

The HttpClientModule in Angular simplifies HTTP communication. After importing it into your app, you inject HttpClient into your service or component. You can then make HTTP requests (GET, POST, etc.) and handle responses with observables, making data retrieval a breeze. 


how HTTP client module works 
    The HttpClientModule in Angular streamlines HTTP operations. First, import it into your app module. Then, inject HttpClient into your service or component. Now, you can make HTTP calls like GET or POST. The module handles requests and gives you observables to work with the response data. 
what are pipes and how to create a custom pipe.

    Custom pipes in Angular let you define your own data transformations. You create them by implementing the PipeTransform interface in a class, with a transform method that modifies the input. Use the @Pipe decorator to name your pipe and then you can use it in templates just like built-in pipes. 

    ViewChild and ContentChild are decorators in Angular used to access elements or components. ViewChild is for elements within the same template, typically for view queries. ContentChild accesses projected content from another component, useful for content queries.

    The async pipe in Angular automatically subscribes to an observable or promise, returning the latest value. It manages subscriptions and auto-unsubscribes when the component is destroyed.

No comments:

Post a Comment