Skip to main content

Posts

ERROR ReferenceError: localStorage is not defined in Angular 17/18

The error ReferenceError: localStorage is not defined occurs in Angular projects when localStorage is accessed in an environment where it is not available, such as on the server side during server-side rendering (SSR) using Angular Universal. The localStorage API is part of the browser's window object, and it is unavailable in non-browser environments. To fix this error in your Angular 18 project, you can use the following approaches: 1. Check if localStorage Exists Ensure localStorage is accessed only in the browser. Modify your service or component code to check for its availability before using it. if ( typeof window !== 'undefined' && window . localStorage ) { // Access localStorage here localStorage . setItem ( 'key' , 'value' ); } else { console . warn ( 'localStorage is not available.' ); } 2. Use Angular's Dependency Injection for Platform Check Use Angular's PLATFORM_ID to check whether the code is running ...
Recent posts

Call child component method from parent class - Angular

Call child component method from parent class - Angular In Angular, to share data from a child component to a parent component, the most common approach is using EventEmitter with @Output() . Here's how it works step by step: Steps for Child-to-Parent Data Sharing In the Child Component : You define an EventEmitter using the @Output() decorator to emit the data. In the Parent Component : The parent component listens for the event emitted by the child and handles the data that is sent. Example: 1. Child Component ( child.component.ts ) Here, we want to send data (e.g., a message) from the child component to the parent. typescript import { Component , Output , EventEmitter } from '@angular/core' ; @Component ({ selector : 'app-child' , template : ` <button (click)="sendData()">Send Data to Parent</button> ` , styleUrls : [ './child.component.css' ] }) export class ChildComponent { // Step 1: Declare an EventEmitt...

What is the difference between Task and Thread in C#

Both Threads and Task are used for Concurrent and Parallel programming. Thread are scheduled by the Operating system while the Task is scheduled by the TPL (Task parallel Library). Threads offer low-level control but require manual management of synchronization while Tasks provide a higher-level abstraction for asynchronous operations and management are automatically.  Threads is synchronous programming while Task is asynchronous programming using async and await keywords. Threads is a lightweight unit of execution that operates independently of other threads.  Threads require manual exception handling (try-catch block) within the thread and no exception aggregation while Task handle exceptions automatically to the calling code and aggregate multiple exceptions if necessary, using AggregateException for multiple errors.  In general, Tasks provide a more modern, structured way to handle errors, especially when dealing with parallelism or asynchronous programmin...

How to Create, Update, Insert and Delete SQL Views? Is it posible to do?

Yes, it is possible to Create , Update , Insert , and Delete data in SQL Views under certain conditions. SQL views allow you to define a virtual table based on the result of an SQL query. They are useful for simplifying complex queries, providing abstraction, and improving security. 1. Creating a View You can create a view using the CREATE VIEW statement. This defines a virtual table based on a SQL query. Syntax: CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition ; Example: CREATE VIEW EmployeeView AS SELECT EmployeeID, FirstName, LastName, Department FROM Employees WHERE Department = 'HR' ; 2. Updating a View To modify an existing view, you can use the ALTER VIEW statement. It allows you to change the definition of the view. Syntax: ALTER VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition ; Example: ALTER VIEW EmployeeView AS SELECT EmployeeID, FirstName, LastName, Department, Salary FROM ...

constructors vs ngOnInit in Angular and .net c# Init

 In Angular, constructors and ngOnInit are both important lifecycle methods, but they serve different purposes and are called at different times. Here's how they work and the order in which they are called: 1. Constructor : The constructor is a method that is invoked first when a component is instantiated. It is used primarily for dependency injection (DI) and initial setup , but it shouldn't contain complex logic or interact with DOM elements since the component isn't fully initialized at this stage. 2. ngOnInit : The ngOnInit() method is part of Angular's component lifecycle hooks and is called after the constructor . It is executed once the component’s input properties are initialized, and is a good place to put logic that depends on the component being fully initialized, like making HTTP requests or setting up component properties based on input values. Order of Execution in Angular: Constructor : Called when the component is created. This is where dependency ...

forkJoin in Angular 17 | RxJS library

 It seems like you are asking about forkJoin in Angular. forkJoin is a method from the RxJS library that allows you to execute multiple observable requests in parallel and wait for all of them to complete before taking action. What is forkJoin ? forkJoin is commonly used when you need to execute multiple HTTP requests (or other observables) in parallel and only proceed once all the observables have completed. It combines the values emitted by each of the observables into a single array (or object) of results, which you can then process. Key Points About forkJoin : It waits for all observables to complete. It emits the last value of each observable. If one of the observables fails , the entire forkJoin will fail (i.e., it will not emit any value). Usage of forkJoin in Angular To use forkJoin in an Angular application, you typically import it from RxJS and use it within a service or component to manage multiple HTTP requests or other asynchronous operations. Example Scenar...

Dependency Injection vs Factory Pattern

The Factory Design Pattern and Dependency Injection (DI) are both design patterns used to manage object creation and dependencies in software development, but they serve different purposes and are used in different contexts. Here's a detailed comparison: Factory Design Pattern : Purpose : Handles object creation. Focus : Centers on how objects are created. Responsibility : The factory class is responsible for creating objects. Coupling : Can result in tighter coupling between the client and the factory. Control : The factory controls which objects are created. When to Use : When object creation is complex or requires abstraction. Testability : Less test-friendly, as it can tightly couple creation logic to the client. Dependency Injection (DI) : Purpose : Provides dependencies to a class. Focus : Centers on how dependencies are provided. Responsibility : The DI container or framework is responsible for injecting dependencies. Coupling : Encourages loose coupling by decoupling ob...

Add Godaddy SSL Certificate to IIS 10 Server | Rekey

How to Add Godaddy SSL Certificate to IIS 10 (Full 2024 Guide)? Installing Godaddy SSL Certificate on IIS 10 Server? Rekey and Install SSL Certificate in IIS 10 Server? Manually Install an SSL Certificate on my IIS 10 Server? Download the SSL Certificate from GoDaddy: ·         Go to your GoDaddy account and navigate to the SSL Certificates section. ·         Find your issued SSL certificate and click Download. ·         Choose the correct server type from the dropdown and download the certificate files (usually a .crt file). Create the .pfx file using the below OpenSSL command prompt: openssl pkcs12 -export -out certificate.pfx -inkey generated-private-key.txt -in 530276e452e0d3fb.crt -certfile 530276e452e0d3fb.crt As per above command: ·         Downloaded the 'Generated Private Key' text file: generated-private-key.txt ·  ...