1

How can I render component in the other component on button click inside the component?

1
  • Please be more specific, it's quite hard to understand what you're trying to accomplish Commented Feb 4, 2019 at 13:21

1 Answer 1

1

I think what you're looking for is EventEmitters. See https://angular.io/api/core/EventEmitter for documentation.

Here's a simple codepen to demonstrate: https://codepen.io/devmaximilian/pen/bzrKag

// parent-component.ts
@Component({
  selector: 'parent-component',
  template: `
   <h1>Parent</h1>
   <child-a-component [showNestedChild]="showNestedChildA"></child-a-component>
   <child-b-component (toggleNested)="toggleShowNestedChildA($event)"></child-b-component>
  `
})
class ParentComponent {
  showNestedChildA: boolean = false;

  public toggleShowNestedChildA(event) {
    this.showNestedChildA = !this.showNestedChildA;
  }
}

// child-a-component.ts
@Component({
  selector: 'child-a-component',
  template: `
   <h2>Child A</h2>
   <nested-child-a-component *ngIf="showNestedChild"></nested-child-a-component>
  `
})
class ChildAComponent {
  @Input() showNestedChild: boolean = false;
}

// nested-child-a-component.ts
@Component({
  selector: 'nested-child-a-component',
  template: `
  <h3>Nested Child A</h3>
  `
})
class NestedChildAComponent { }

// child-b-component.ts
@Component({
  selector: 'child-b-component',
  template: `
   <h2>Child B</h2>
   <button (click)="showNested()">Toggle Nested Child A</button>
  `
})
class ChildBComponent {
  @Output() toggleNested = new EventEmitter();

  public showNested() {
    this.toggleNested.emit(null)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.