How to pass variable value from one component to another in angular 2


Vishu

app.component.ts

@Component({
  moduleId: module.id,
  selector: 'mx-itool-clock',
  template: `<leftpanel></leftpanel><workmat></workmat>`,
  directives: [ClockWorkmat,ClockLeftPanel]
})

export class AppComponent{
}

leftpanel.component.ts

@Component({
  moduleId: module.id,
  selector: 'clock-leftpanel',
  template: `<div class="show-time">{{time}}</div>',
})
export class ClockLeftPanel {
    time:number=10;
}

workmat.component.ts

@Component({
    moduleId: module.id,
    selector: 'clock-workmat',
    template: '<div>{{time}}</div>',
})
export class ClockWorkmat {
  time:number;
}

I want to get time value from leftpanel.component.ts and assign to time in workmat.ts

Can anyone tell me how to implement it in a simple way using angular 2 rc4.

Yonatan Doron

If two child components of the parent "app" component needed to access such a variable, I would have liked to keep all logic related to that variable under the parent component, so "app" in your case.

If for some reason it conflicts with your app design, and you mostly put this variable under left panel components, there are various solutions available to fix this.

The easiest solution I've seen is to implement NgRedux and set it up in your project so you can distribute updates to some state that holds a record of that variable and has easy access to anywhere around it value of . that state.

If you're new to NgRedux and want something less efficient but out of the box, you can do it the Angular way by importing: Output, EventEmitter in the left panel comp, and fire events wherever you want (ngOnInit / when activating the function, example), then import Input in your app component. Naturally, the parent will listen to this event, and when fired will activate a function that will now only update the private variable in the "app" component, like this:

import { Component, Output, EventEmitter } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'letf-panel',
    template: ` 
               <div>Left Panel Content</div>
    `
})
export class LeftPanelComponent {
    @Output() timeEvent = new EventEmitter();

    constructor() {}
}

Then for example under NgOnInit (inside the LeftPanelComponent class), emit an event with a time variable:

ngOnInit() {
   this.timeEvent.emit(time)
}

You enter events like this:

import { Component, Input } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'app',
    template: '<div (timeEvent)="getTime()"></div>',
})
export class AppComponent{

  @Input() time:number;
  private timeInApp;

  getTime(timeFromLeftPanel) {
      this.timeInApp = timeFromLeftPanel;
  }
}

Once the variable is included in the app comp, it can be bound to the workmat component like this:

[time]="time"

and in the template:

template: `<leftpanel #src></leftpanel><workmat [time]="time"></workmat>`

Let me know if this helps you

Related


Angular 2 pass variable from one component to another

Andreas L. So I have a component that contains an array of lists: A component TS: list = [ {name:Jhon}, {name: Pop} ] A component html: <h2>List :</h2> <b-component></b-component> //how to pass list to B component here ? B component html : <div *ngFor="let i

How to pass objects from one component to another in Angular 2?

Anoush Hakobyan: I have Angular components , the first component uses the second component as a directive . They should share the same model object , which is initialized in the first component. How can I pass that model to the second component? Shandmani: Com

How to pass data from one component to another in angular 2?

N I have two components (ComponentOne and ComponentTwo) in an angular 2 application. ComponentOne needs user details and user preferences to perform some actions, so when ComponentOne is initialized, I will get these details from a backend service. @Component(

How to pass objects from one component to another in Angular 2?

Anoush Hakobyan: I have Angular components , the first component uses the second component as a directive . They should share the same model object , which is initialized in the first component. How can I pass that model to the second component? Shandmani: Com

How to pass objects from one component to another in Angular 2?

Anoush Hakobyan: I have Angular components , the first component uses the second component as a directive . They should share the same model object , which is initialized in the first component. How can I pass that model to the second component? Shandmani: Com

How to pass data from one component to another in angular 2?

N I have two components (ComponentOne and ComponentTwo) in an angular 2 application. ComponentOne needs user details and user preferences to perform some actions, so when ComponentOne is initialized, I will get these details from a backend service. @Component(

How to pass value from one component to another

Jannemannen : I have this prop in one of my components and I want to pass it to another component and use it as a variable there: const VerContinentToolbar = (props) => { return ( <Menu className="nav-icon"> <CountryList displayFields={["count

How to pass value from one component to another

Jannemannen : I have this prop in one of my components and I want to pass it to another component and use it as a variable there: const VerContinentToolbar = (props) => { return ( <Menu className="nav-icon"> <CountryList displayFields={["count

Pass data from one component to another Angular 2

J people I have a header component that uses a "User Service" class to call an HTTP GET function to retrieve JSON data from a REST endpoint. The service is working fine and I can get the data, but I want that data to be able to determine which controls are dis