0

My HTML is creating dynamic divs with ngFor

    <div class="container">
      <div class="scroll" scrollX="true" >

        <div *ngFor="let i of lotes" class="card" id="card_{{i.id_lote}}">
          <div class="title">
            {{ i.nome_lote }}
          </div>
        </div>

      </div>
    </div>

Then I'm looping an array with ID's and trying to set click listeners on those divs:

  ngAfterViewInit() {
    this.lotes.forEach(function(lote){

      let card_instance = document.getElementById('card_'+id_lote);
      console.log(card_instance);     // <==================== this works and shows correct element

      card_instance.addEventListener("click", (evt) => {
        console.log('aaaahhhhh');
      });

    });
  }

So I can reach the element but when I click it nothing happens. It looks like the addEventListener don't work...

Am I doing this the right way?

1 Answer 1

2

you can use the click on the div like this:

 <div class="title" (click)="yourFunction(i)">
            {{ i.nome_lote }}
          </div> 

and in your component just create a function like this

ngAfterViewInit() {}

 yourFunction(loteThatWasClicked): void{
    console.log(loteThatWasClicked);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.