Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner HTML of ng-container, Angular 4?

Tags:

html

angular

I want to dynamically place an html code in my html code, So I write the following code:

<ng-container [innerHTML]="buttonIcon"></ng-container>

Angular says innerHTML is not valid attribute for ng-container
I don't want to use third html tag like follows:

<div [innerHTML]="buttonIcon"></div>

So how can I insert html codes without any tag inner html binding?

like image 388
Mohammad Dayyan Avatar asked Sep 09 '17 19:09

Mohammad Dayyan


People also ask

Can I use innerHTML in Angular?

Angular 2+ supports an [innerHTML] property binding that will render HTML. If you were to otherwise use interpolation, it would be treated as a string. In this article, you will be presented with how to use [innerHTML] and some considerations for usage.

What is Ng-container HTML?

What is ng-container ? ng-container allows us to create a division or section in a template without introducing a new HTML element. The ng-container does not render in the DOM, but content inside it is rendered. ng-container is not a directive, component, class, or interface, but just a syntax element.

Can we use ng-container inside Ng container?

To sum up, ng-content is used to display children in a template, ng-container is used as a non-rendered container to avoid having to add a span or a div, and ng-template allows you to group some content that is not rendered directly but can be used in other places of your template or you code.

Can I apply style to Ng-container?

According to Angular official docs <ng-container> is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM. It's only meant to be used as a container for structural directives when you need some HTML elements immediate children to be of a specific type.


4 Answers

[outerHTML]

will do the trick to replace the outer element.

In your case

<div [outerHTML]="buttonIcon"></div>

It's true, that it's important to have a clean HTML structure for e.g. keeping CSS rules as simple as possible.

like image 54
Pierre Chavaroche Avatar answered Oct 06 '22 00:10

Pierre Chavaroche


You can use ngTemplate:

<ng-template #buttonIcon>
    <div> Your html</div>
</ng-template>
<ng-container 
   *ngTemplateOutlet="buttonIcon">
</ng-container>
like image 35
Bhojendra Rauniyar Avatar answered Oct 05 '22 23:10

Bhojendra Rauniyar


** Please read the comments. This answer might be wrong. I dont know, have not looked into it again **

ng-container does not get rendered to html, it is a mere structural directive.

The Angular is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM.

So there is no element to put html into. You need to work with a sub-div. If there is no need for a sub-div in your opinion, then you could most probably also just replace ng-container with div itself and not use the container at all.

like image 39
phil294 Avatar answered Oct 05 '22 23:10

phil294


If for any reason you need to replace the DOM element you can use a div with an id and then use the @ViewChild decorator and ElementRef to get access to the nativeElement from the controller and set the outerHtml property.

app.component.tml

<div #iconButton></div>

app.component.ts

import { Component, ViewChild, ElementRef, ViewEncapsulation, AfterViewInit }from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  encapsulation: ViewEncapsulation.None
})
export class AppComponent  implements AfterViewInit{
   @ViewChild('iconButton')
    iconButton: ElementRef;

    ngAfterViewInit(){
      this.iconButton.nativeElement.outerHTML = '<button>My button</button>'
    }
}

We need to use none as encapsulation policy because our template only includes the div to be replaced.

Stackblitz example: https://stackblitz.com/edit/angular-fa1zwp

like image 27
Javier Rojano Avatar answered Oct 06 '22 00:10

Javier Rojano