I
I
iluxa18102019-03-20 18:29:37
Angular
iluxa1810, 2019-03-20 18:29:37

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

2 answer(s)
D
Dmitry Luzanov, 2019-03-20
@dmitry_luzanov

It is possible, but not necessary. It will backfire if you then screw SSR or WebWorker. Use Renderer2 instead

M
msdosx86, 2019-03-23
@msdosx86

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 question

Ask a Question

731 491 924 answers to any question