Angular Experienced Questions
Published: 2023-10-17 18:22:51





![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzule7qvchjrjpiq2ky0.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6lqtoqy2aoq6dsj4n8s.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7m9rnit7p5rsbr3b3c4l.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7haml95duy5bobe4bkbk.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ggz4x3fqwt79r4pk1eb1.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezg6b6cshxtkec6e6zhe.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22460c86zgzdddvsc8v0.png)














## Currying

```javascript
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


```








```javascript

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);
```


## 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.

2. **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.

3. **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.

4. **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.

5. **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.

6. **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).