0

I have 4 components on my Angular 2 app. Components are Header, Footer, Navigation and Content. I have a button in the header component and I want to show/hide the content in the navigation component when user click on the button from the header component. I want to know that when I click the button from the header how to pass the Boolean value from header component to the navigation component. All the components have their own html templates. Let me know what is the way to pass the toggle value from header to navigation component.

Thanks

1
  • 1
    Please add the code that shows your components, their template and how they are related. Commented Apr 1, 2016 at 6:51

1 Answer 1

2

You can take advantage of sharedservice and sharedobject as shown below.

working demo

sharedService.ts

export interface ImyInterface {
   show:boolean;
}

@Injectable()
export class sharedService {
  showhide:ImyInterface={show:true};

  hide(){
        this.showhide.show=!this.showhide.show;
  }
} 

header.ts (content.ts)

import {Component,Injectable} from 'angular2/core';
import {sharedService} from 'src/sharedService';

@Component({
  selector: 'thecontent',
    template: `
    <div>Header Component <button (click)=showhide()>show/hide</button></div>
    `
})
export class TheContent {
  constructor(private ss: sharedService) {
    console.log("content started");
  }
  showhide() {
    this.ss.hide();
  }
}

navigation.ts (nav.ts)

import {Component,bind} from 'angular2/core';
import {sharedService} from 'src/sharedService';

@Component({
  selector: 'navbar',
  template: `
  <style>
    .bk{
          background-color:black;
          color:white;
    }
  </style>
  <div>Navigation Component </div>
  <div [class.bk]="true" *ngIf="showHide.show"> Showing </div>
  <hr>
  <hr>
  `
})
export class Navbar {
  showHide:ImyInterface;
  constructor(ss: sharedService) {
    this.showHide=ss.showhide;
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the solution. I tried the code which you provided. I am getting this error EXCEPTION: TypeError: Cannot read property 'show' of undefined in [showHide.show in edockNavigationComponent@3:32]
have you written this line in export class Navbar{ showHide:ImyInterface; ... ... ...} ? and same interface in sharedService. Check my demo properly.
Very nice. I like that you use an API, hide(), rather than modifying the show property directly in showhide(). I think I like the usage of an interface. This seems more efficient than using another API on the service to get the value. However, if you did use an API to get the current value of show (e.g., *ngIf="ss.getShowValue()), then you wouldn't need to wrap the boolean in an object. I'm not sure which I like better... efficiency or more encapsulation. Your thoughts?
@MarkRajcok glad thtat you like it. But can you please fork plunk n show me what do you want? (as there can be different ways)
It's not "what I want", but more like... what do you think of this approach?: plnkr.co/edit/n7MxhJoLDCLZbl4mOxjX?p=preview I call a function on the service in the NgIf expression, rather than rely on a shared reference type.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.