How to add an Angular property to an HTML element

How to add an Angular property to an HTML element



I need to know how to add to an html button the property (click) = function() of angular through Javascript.


(click) = function()



Note: I cannot modify the HTML, I can only add the property through JavaScript.



I tested with addEventListener and it works by adding the common JavaScript click = "function" event, but not the (click) of Angular.


click = "function"


(click)



I attach the code:


import Component, OnInit from '@angular/core';

@Component(
selector: 'app-iframe',
templateUrl: './iframe.component.html',
styleUrls: ['./iframe.component.scss']
)
export class IframeComponent implements OnInit
constructor()

ngOnInit()


capture()
let button = document.getElementById('cancelButton').addEventListener('(click)', this.cancel.bind(Event));


cancel()
console.log('Cancelled');




And the HTML here:


<div class="row text-center pad-md">
<button id="acceptButton" mat-raised-button color="primary">OK!</button>
<button id="cancelButton" mat-raised-button>Cancel</button>
</div>





Since you cannot modify the HTML, you will have to use addEventListener("click", this.cancel.bind(this)). Is it a problem?
– ConnorsFan
Aug 21 at 19:19



addEventListener("click", this.cancel.bind(this))





Why cannot you change the HTML?
– Batajus
Aug 21 at 19:22





i've tried with addEventListener("click", this.cancel.bind(this)) and it aplies the 'common' click property of vanilla Javascript, but not the (click) reagarding to the Angular directive.
– Jgascona
Aug 21 at 19:25





And I cannot modify the HTML because it is retrieving from an external URL
– Jgascona
Aug 21 at 19:26





Firstly, is the iframe loading a page from the same domain?
– zero298
Aug 21 at 20:04




1 Answer
1



As stated by the author, the event need to be attached dynamically to the DOM element that is created after a request, so you can use Renderer2
to listen for the click event. Your code should look like this:


import Component, OnInit, Renderer2 from '@angular/core';

@Component(
selector: 'app-iframe',
templateUrl: './iframe.component.html',
styleUrls: ['./iframe.component.scss']
)
export class AppComponent implements OnInit
name = 'Angular';

constructor(private renderer: Renderer2)

ngOnInit()

capture()
const button = document.getElementById('cancelButton');
console.log(button);
this.renderer.listen(button, 'click', this.cancel);


cancel()
console.log('Cancelled');




There's a functional example here.





Yes I tried that before, and it works in order to add the "common" click event of HTML to the object. But as I said before, I need to append the (click) directive of angular, not the click event of HTML. Because the function is in the component of Angular and I cannot access to these function from the common click of HTML.
– Jgascona
Aug 22 at 6:57





You're correct! I've edited the answer, try that instead.
– Túlio Oliveira
Aug 22 at 12:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)