How to insert HTML within an Angular 4 Component?

Asked

Viewed 3,047 times

2

Hello! How to insert an HTML code into an Angular Component? For example:

I have the Component button:

whose code is:

<button type="submit"></button>

I would like to insert the html in this way:

<app-button>Teste</app-button>

so that the Component code looks like this, dynamic:

<button type="submit">Teste</button>

I know it’s possible, but I haven’t found it yet. Grateful from now on.

  • It’s not duplicated. This topic you mentioned teaches the basic component creation, which I claimed to know in this topic. In case my question is about how to dynamically insert HTML into the component.

  • 1

    In the example template: <button type="submit">{{name}}</button>, in HTML <app-button name="ENVIAR"></app-button>, in the add Component within the class @Input() name:string; and import Input

2 answers

5


You’re looking for the tag ng-content it allows you to access what was passed in the body of the.

Example:

import {Component} from '@angular/core';

@Component({
  selector: 'custom-component',
  template: `<button>
              <ng-content></ng-content>
              </button>`,
})
export class CustomComponent{

}

Using the above component, you can create it and pass a text in your body.

<custom-component>BOTÃO CUSTOMIZADO </custom-component>

Angular will render the following html:

<custom-component>
   <button> BOTÃO CUSTOMIZADO </button>
</custom-component>

I created an example in stackblitz for you

  • I didn’t know this method. Cool to know. But I still prefer to use it as in my answer, because then I have a better control of the template. Would you have an example that this method is better? Or something that indicates when it is best to use this method?

  • @celsomtrindade In my opinion it makes more sense to use the ng-content to pass an html into your component’s template. In the case of the question (Very simple scenario), it actually makes more sense to use the @Input(). A good example is the Material library that uses ng-content in its components, for example mat-card

  • Yeah, in scenarios with a more complex template, really, the ng-content would be better. Whenever I needed to do something like this I preferred to use a new component when submitting a template. But it was nice to know this method.

4

You must create a variable to receive the values within the Component. See this example:

@Component({
    selector: 'app-button',
    template: `
        <button type="submit">{{label}}</button>
    `,
})

export class AppButtonComponent {
    @Input() label: string;
}

This will make you need to declare the variable label the moment you create the component, like this:

<app-button [label]="'Teste'"></app-button>

Note that I declared the name "Test" in single quotes, as I am declaring it manually. If it were another variable inside your other Komponent, you could pass the variable directly, without the need for quotation marks, like this:

<app-button [label]="minhaVariavel"></app-button>

Observing: You cannot declare anything within the declaration of app-button because, when rendering, Angular will replace what you manually declared with what was declared in the property template or templateUrl.

  • I got it. In some projects I saw something like: <div class="panel"><div class="panel-title">Test</div><div class="panel-body">Content</div></div>. This was the component template. They did something like <app-Theme-panel>Content</app-Theme-panel>. In this case what was between the tags entered the div body of the panel. The code so became more summarized. Something like the slot and Yield in the Laravel, you know? How was that possible?

  • @Wallacemagalhães do you have any example of this? Because I’ve never seen this type of interaction in Angular. The most I know that it is possible to do would be to declare templateUrl, that you create a file .html with your template and Angular will compile to Javascript. In Angularjs (version 1) I know that it was possible something similar to this (I don’t know if it is yet because I haven’t used it for some time now).

  • Yes, look here: https://mika-el.github.io/angular-admin-lte/#/Alert look at the Alert code. It kind of inserts html between component tags

Browser other questions tagged

You are not signed in. Login or sign up in order to post.