Passing Data From Child Component Into Parent Component

nauval

I have problems with my angular, here I have two components:

  • MyApp (ParentComponent)
  • LoginPage (ChildComponent)

I have a property UserNow in parts MyApp and I want to set the value of the property UserNow through the components LoginPage. How to do it?

I have tried (but did not give any influence)

Import {MyApp} from '../../app/app.component'; 

@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {
    public app: any;

    login() {
        ...
        this.app = MyApp;
        this.app.userNow = MyValue;
        ...
    }
}
SaUrAbH MaUrYa

There are several ways to do it.

1) Using a service: A service generally has a single instance in the application and can be used to share data between the components easily. E.g. create a service userService and inject it in components where ever you want to use it.

2) using Emit: Emit is used to emit an event in the application and corresponding action can be taken.

this.eventInChild.emit(data);

Two actions can be taken on event emission.

  • calling a function of parent :

<child-component (eventInChild)="parentFunction($event)"></child-component>

  • Emitting from service and Subscribing to an event(can be subscribed in service as well as components) :

In Service It goes like this:

getEmitStatus() {
    return this.eventInService;
}

//In component or service - to listen to event

this.subscription = this.userService.getEmitStatus()
    .subscribe(item => {
         //thing to do here
}); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing data from Parent Component to Child Component

passing data from child to parent component in Vue

Passing data from child to parent component in React

Passing data from child component to parent component in react native?

Passing data from service to parent then from parent to child component

ReactJS Passing data from child component, to parent, back to different child

Passing data from child component

passing data from child to parent component - react - via callback function

Vuejs Passing dynamic data from parent to child component

Passing API data from parent container to child component in React

Angular 2 Passing data from Parent component to child

ReactJS - passing data from a child component to its parent

Passing data from child to parent component not working in react

Passing data from parent to child component (MaterialUI make styles)?

Error while passing data from a child to parent component in reactjs

Data is delayed by one while passing from child component to parent

Child component is not getting data from the parent component

Passing the value from the child to parent component and then to the child component again

Passing parent component data to child via routerLink

Passing child component data to parent using props

Error with passing a prop from parent to child component

Passing state from parent component to child function

NativeScript - Passing content from Parent to Child component

Passing props from parent component to child components

Angular 16, parent component is not passing data to the second child component

HTML error when Passing data from Parent component to Child Component in Angular

React router dom passing data from parent component to child router component does not pass the props.match

Angular2 Routing - Passing data from parent component to a sub-child component

Is passing a piece of data from child component to parent component an anti pattern in react?