Error creating an attribute with Angular 6

0

I want to create an attribute that calls (click) from setAtributte (), but it tells me this error:

ERROR DOMException: Failed to execute 'setAttribute' on 'Element': 
'(click)' is not a valid attribute name.

Search all over Google and I'm not finding the solution, I know it's the parentheses but I need to make the angular execute a function. I do not want to have to create an onClick attribute to then have to create a Jquery file and from there execute the method.

Part of the code and where the error is applied:

this.renderer.appendChild(this.el.nativeElement, div);
      this.renderer.setAttribute(button, 'type', 'button');
      this.renderer.setAttribute(button, 'data-toggle', 'modal');
      this.renderer.setAttribute(button, 'data-target', '#modalArbol');
      this.renderer.setAttribute(button, '(click)', 'mostrarId()');
      this.renderer.addClass(button, 'wild');
      this.renderer.addClass(button, 'btn-primary');
    
asked by Nahuel Jakobson 13.11.2018 в 00:43
source

1 answer

1

Good morning, colleague! I understand why you say "create a Jquery file and from there execute the method" I had an almost similar since the function that you add through addEventlistener is executed in a context different from the class of the component. But you can do this:

.addEventListener('click', this.nombreDeFuncionEnComponente.bind(this, [parametros]));

And already with it you can execute a function that is inside the class of the component in which you are working. By [parameters] I mean this:

// Esta es la función en tu component.ts
nombreDeFuncionEnComponente(nombre:string, edad:number) {}

.addEventListener('click', this.nombreDeFuncionEnComponente.bind(this, 'Sergio', 28));

Try and tell me!

    
answered by 26.11.2018 в 00:35