Answer the question
In order to leave comments, you need to log in
Is it possible to work with the DOM directly using Angular?
Is it possible to work with the DOM directly using Angular?
Let's say I want to click on a button and then add something to the DOM myself.
How can I do this in a component?
If so, please give a very simple example.
Answer the question
In order to leave comments, you need to log in
It is possible, but not necessary. It will backfire if you then screw SSR or WebWorker. Use Renderer2 instead
That is unnecessary. If you want to add, for example, an element to an array on the click of a button, then just add it. Angular will do everything for you. This is the whole point of frameworks. They let you think about the business logic, not the implementation details.
<div *ngFor="let item of items"> {{ item }} </div>
<button (click)="addItem()">Add</button>
items = [1, 2, 3, 4];
addItem(): void {
this.items.push(5);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question