DEV Community

varma36a
varma36a

Posted on • Updated on

Angular Experienced Questions

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Currying

function sum(x){
      return function(y){
        return (y!=undefined)?sum(x+y):x;
      }

}
console.log(sum(10)(20)())

sum(10)(20)()
sum(30)()


returns 30


Enter fullscreen mode Exit fullscreen mode

function outerFunction(outerVariable) {
  // This inner function is a closure
  function innerFunction(innerVariable) {
    return outerVariable + innerVariable;
  }

  return innerFunction;
}

// Create a closure by calling outerFunction with an argument
const closure = outerFunction(10);

// Use the closure to access outerVariable
const result = closure(5); // result is 15

console.log(result);
Enter fullscreen mode Exit fullscreen mode

pass data to sibling components in angular

parent has 2 childs

we need to pass from child to child

  • we use subject to communicate with multiple components

  • A subject is a special observable which acts as both observer and observable. They allow us to emit new values to the observable stream using the next method all the subscribers who subscribe to the subject will receive the same instance of subject and hence the same values

  • it can emit the data and subscribe as well.

In Angular, Subject and BehaviorSubject are two commonly used classes provided by the RxJS library to manage and propagate state and data within an application. They are often used for inter-component communication and managing shared application state. Here are some use cases for both Subject and BehaviorSubject:

  1. Inter-Component Communication:
    • Subject: You can use a Subject to create an event bus or a central messaging system for components to communicate with each other. For example, you can notify multiple components when a user logs in or out by subscribing to the same Subject.
  • BehaviorSubject: A BehaviorSubject can be used to share a piece of state among components. For example, you can use a BehaviorSubject to manage the current user's authentication state so that any component that subscribes to it always gets the latest state.
  1. State Management:
    • Subject: You can use a Subject to manage transient state that doesn't need an initial value. For instance, if you have an event-based system and you want to notify components of certain events, a Subject can be used.
  • BehaviorSubject: Use a BehaviorSubject when you need to manage a state with an initial value that components should immediately receive upon subscription. For example, you can use a BehaviorSubject to manage the state of a shopping cart with an initial set of items.
  1. Form Data Sharing:
    • Subject: You can use a Subject to share form data among components. For example, when a form is filled out in one component, you can use a Subject to notify other components about the form data changes.
  • BehaviorSubject: If you want to ensure that new subscribers immediately receive the current form data when they subscribe, a BehaviorSubject can be used. This is useful when components need access to the form data upon initialization.
  1. Caching API Responses:
    • Subject: You can use a Subject to create a data cache for API responses. When one component fetches data from an API, it can notify a Subject, and other components can subscribe to it to access the cached data.
  • BehaviorSubject: If you want to ensure that new subscribers immediately receive the most recent API response data, you can use a BehaviorSubject. This is helpful when multiple components depend on the latest API data.
  1. User Authentication State:

    • BehaviorSubject: Managing user authentication state is a common use case for BehaviorSubject. You can use it to store the user's authentication status and propagate it to various parts of the application. When a component subscribes, it can immediately access the user's authentication state.
  2. Navigation and Route Handling:

    • Subject: You can use a Subject to handle navigation and route changes. When a component needs to trigger a route change, it can notify a Subject, and other components (e.g., navigation components) can subscribe to it to react accordingly.

These are just a few examples of how Subject and BehaviorSubject can be used in Angular. The choice between the two depends on your specific requirements, especially whether you need to provide an initial value to subscribers (which is the key difference between them).

Top comments (0)