What I want to do is simply to append some html on an element. I checked some links and found different confusing, non-working, non-recommended solutions.
Using JavaScript, I'll do something like this:
var d1 = document.getElementsByClassName('one');
d1.insertAdjacentHTML('beforeend', '<div class="two">two</div>');
How do I achieve the same result using typescript/angular2, RC5?
EDIT
The element with class .one
is generated by an external js, and I can't modify it.
1.
<div class="one" [innerHtml]="htmlToAdd"></div>
this.htmlToAdd = '<div class="two">two</div>';
See also In RC.1 some styles can't be added using binding syntax
<div class="one" #one></div>
@ViewChild('one') d1:ElementRef;
ngAfterViewInit() {
d1.nativeElement.insertAdjacentHTML('beforeend', '<div class="two">two</div>');
}
or to prevent direct DOM access:
constructor(private renderer:Renderer) {}
@ViewChild('one') d1:ElementRef;
ngAfterViewInit() {
this.renderer.invokeElementMethod(this.d1.nativeElement', 'insertAdjacentHTML' ['beforeend', '<div class="two">two</div>']);
}
constructor(private elementRef:ElementRef) {}
ngAfterViewInit() {
var d1 = this.elementRef.nativeElement.querySelector('.one');
d1.insertAdjacentHTML('beforeend', '<div class="two">two</div>');
}
With the new angular class Renderer2
constructor(private renderer:Renderer2) {}
@ViewChild('one', { static: false }) d1: ElementRef;
ngAfterViewInit() {
const d2 = this.renderer.createElement('div');
const text = this.renderer.createText('two');
this.renderer.appendChild(d2, text);
this.renderer.appendChild(this.d1.nativeElement, d2);
}
You could do something like this:
htmlComponent.ts
htmlVariable: string = "<b>Some html.</b>";
//this is html in TypeScript code that you need to display
htmlComponent.html
<div [innerHtml]="htmlVariable"></div>
//this is how you display html code from TypeScript in your html
There is a better solution to this answer that is more Angular based.
Save your string in a variable in the .ts file
MyStrings = ["one","two","three"]
In the html file use *ngFor.
<div class="one" *ngFor="let string of MyStrings; let i = index">
<div class="two">{{string}}</div>
</div>
if you want to dynamically insert the div element, just push more strings into the MyStrings array
myFunction(nextString){
this.MyString.push(nextString)
}
this way every time you click the button containing the myFunction(nextString) you effectively add another class="two" div which acts the same way as inserting it into the DOM with pure javascript.
When working with Angular the recent update to Angular 8 introduced that a static
property inside @ViewChild()
is required as stated here and here. Then your code would require this small change:
@ViewChild('one') d1:ElementRef;
into
// query results available in ngOnInit
@ViewChild('one', {static: true}) foo: ElementRef;
OR
// query results available in ngAfterViewInit
@ViewChild('one', {static: false}) foo: ElementRef;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With