angular-interview-questions icon indicating copy to clipboard operation
angular-interview-questions copied to clipboard

What is ng-content?

Open Shashidharknaik opened this issue 3 years ago • 2 comments

Please add question & answer about ng-content.

Shashidharknaik avatar Dec 26 '21 06:12 Shashidharknaik

@Shashidharknaik Can you create PR?

sudheerj avatar Jul 31 '22 18:07 sudheerj

Example:

my-button.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'my-button',
  template: `
    <button class='action-btn' (click)="action()">
      <ng-content></ng-content>
    </button>
  `
})

export class MyButtonComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
  action(){
    console.log("action triggered")
  }
}

app.component.html

<my-button>
  Button 1
</my-button>

<my-button>
  Button 2
</my-button>

allows you to add dynamic content inside a custom component.

Using , we can create the same with different labels.

helps us achieve transclusion in Angular.

saheel-ahmed avatar Nov 12 '22 20:11 saheel-ahmed