0

I'm trying to create an Angular reusable button component. Right now, it is used like so:

<fun-button
  [disabled]="true">
  [label]="'button label'"
>
</fun-button>

The template for fun-button looks like this:

<button>
  {{this.label}}
</button>

So far, so good. Everything works.

Now I would like to modify fun-button so that it is used more like an HTML button:

<fun-button>
  child button label
</fun-button>

How can I access fun-button's DOM children, which in this case is the string 'child button label'?

Is such a thing even possible? I've seen a bunch of answers that involve tagging the child elements, but in my case I want to accept any arbitrary children.

Thanks!

EDIT, WITH ANSWER:

Turned out to be incredibly easy, thanks MikeOne. Just made the template look like this:

<button>
  <ng-content></ng-content>
</button>

And then use like this:

<fun-button>
  child button label
</fun-button>
3
  • <ng-content> is your friend..
    – MikeOne
    Commented Mar 31, 2022 at 21:57
  • @MikeOne. Well, that was easy. Thanks! That's what they get for making a guy who's never done Angular do angular.
    – crowhill
    Commented Mar 31, 2022 at 21:59
  • You should invest time in it, it’s a great framework! Happy coding.
    – MikeOne
    Commented Mar 31, 2022 at 22:20

1 Answer 1

1

Made the template look like this:

<button>
  <ng-content></ng-content>
</button>

And then use like this:

<fun-button>
  child button label
</fun-button>

Not the answer you're looking for? Browse other questions tagged or ask your own question.