Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 access variables from another component

Tried EventEmitter but no chance and so little documentation... Any help appreciated

I have a component called sidebar and another one called header, when you click on a button from the header, it should hide the sidebar... How would you achieve this in angular2 ?

thanks

like image 533
kfa Avatar asked May 19 '16 13:05

kfa


People also ask

How do you access a variable from another component?

Go to your app. module. ts and make the provider of that component you want to inherit with other classes: Ex: providers: [DatePipe, AccountComponent] Then go to the class you want access of that variable, and import the component into that: Ex: import { AccountComponent } from '../account/account.

How can we use selector of one component in another?

To use selector as directive, change is required in the selector of the child component. Selector name need to be closed in square bracket, just like in case of property binding. Now the child component can be included in the parent using directive like this.

How do you pass data from one component to another in Angular on button click?

This can be done via input binding, which passes data from one component to another, generally from parent to child. This custom input binding is created by using the @Input() decorator.

How do you use one component in another component in Angular 7?

In Angular mainly we have 4 methods by which a component can share data and information with another component by passing data or events : Components can communicate with each other in various ways, including: Parent to Child: via Input. Child to Parent: via Output() and EventEmitter.


1 Answers

This is pretty easy with a Service you share between your Components.

For instance a SidebarService:

@Injectable()
export class SidebarService {
  showSidebar: boolean = true;

  toggleSidebar() {
    this.showSidebar = !this.showSidebar;
  }
}

In your sidebar component just put a *ngIf with the showSidebar variable from the SidebarService. Also don't forget to add the service in the constructor.

@Component({
  selector: 'sidebar',
  template: `
      <div *ngIf="_sidebarService.showSidebar">This is the sidebar</div>
  `,
  directives: []
})
export class SidebarComponent {
  constructor(private _sidebarService: SidebarService) {

  }
}

In the component, where you want to handle the toggling of the sidebar also inject the SidebarService and add the click event with the service method.

@Component({
  selector: 'my-app',
  template: `
    <div>
      <button (click)="_sidebarService.toggleSidebar()">Toggle Sidebar</button>
      <sidebar></sidebar>
    </div>
  `,
  directives: [SidebarComponent]
})
export class App {
  constructor(private _sidebarService: SidebarService) {

  }
}

Don't forget to add the SidebarService to the providers in your bootstrap:

bootstrap(App, [SidebarService])

Plunker for example usage

like image 124
rinukkusu Avatar answered Oct 28 '22 06:10

rinukkusu