Removing click functionality in Angular 2 when utilizing [innerHTML]

Currently, I need to include HTML in my TypeScript file using the [innerHTML] tag within the template.

I am attempting to add a click function within the HTML:

status = "<img src='assets/hello.png' (click)='hello()' />";

However, the click function is removed and a warning message appears in the console:

WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).

I have seen suggestions about using "DomSanitizer" but haven't come across an example that addresses my specific issue.

Answer №1

When utilizing [innerHTML], Angular will remove all specific Angular elements.

If functionality like this is needed, you have the option to dynamically create components as described in Equivalent of $compile in Angular 2
Alternatively, you can utilize imperative code to attach an event handler like so:

constructor(private elRef:ElementRef, private cdRef:ChangeDetectorRef) {}

someMethod() {
  this.status = "<img src='assets/hello.png' (click)='hello()' />";
  this.cdRef.detectChanges();
  this.elRef.nativeElement.querySelector('img').addEventListener('click', this.hello.bind(this);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve the data from a specific section when a checkbox is selected in Angular 2?

When I select a checkbox for any section and then click the submit button, I want to display the details of that section in the console. Can someone assist me with this? **Stackblitz link:** : https://stackblitz.com/edit/angular-q7y8k1?file=src%2Fapp%2Fa ...

Harnessing the power of external Javascript functions within an Angular 2 template

Within the component, I have a template containing 4 div tags. The goal is to use a JavaScript function named changeValue() to update the content of the first div from 1 to Yes!. Since I am new to TypeScript and Angular 2, I am unsure how to establish comm ...

Compiled TypeScript files are missing require statements for imported components

Recently delved into Angular 2 and encountered an unusual issue. I kicked off using the Angular 2 Quickstart repository on GitHub and incorporated some components with templates. For example: import { Component } from '@angular/core'; import { ...

Angular only allows click events to trigger during a push

Is there a way to reveal the password without requiring a click, but simply by pushing on an eye icon? My code HTML <input [formControlName]="'password'" [type]="isShow ? 'text' : 'password'" class=&qu ...

Showing the Enum name rather than the value in an Angular HTML template for a bound Typescript Interface

After retrieving an array of objects with various properties from a .NET Controller, I am utilizing the following Typescript code: import { Component, Inject } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Co ...

Ways to confirm if the indexed sort is an extension of a string

Suppose I have a function called func with 2 generic arguments const func = <T extends {}, K extends keyof T>() => {}; and a type defined as interface Form { a: boolean; b: string; } then I can call them without encountering any errors func& ...

Exploring the Functionality of Backend Objects in Frontend TypeScript within the MEAN Stack Environment

Utilizing MongoDB, express.js, angular4, node.js Although a string I retrieve is successful, it's not the same as retrieving the full object... account.service.ts (full, ) import { Injectable } from '@angular/core'; import { Http, Headers ...

Error encountered in app.module.ts file of Angular 2 application

My friends and I are working on a big school project, creating a cool web app. Suddenly, I encountered some errors in my app.module.ts file that I haven't seen before. It's strange because they are showing up out of nowhere! The error: Error:( ...

What steps are involved in building a modal pop-up in Angular without using any pre-existing libraries

I am looking to create a modal popup when a button is clicked. I want to achieve this without relying on any additional dependencies. The method I have used below successfully creates a fully functional modal, but I am unsure if this is the best way to do ...

Exploring Angular 2: Diving into Validators, ReactiveForms, FormBuilder, and tailored classes

If you have a class called User with properties username and password export class User { username = ''; password = ''; } To create a reactive form, you can use the following syntax this.userForm = this.fb.group({ usernam ...

Retrieving the value from the series object and showing it in the tooltip of a high chart with the help of Angular 4

I have successfully implemented the display of x and y values in the tooltip of a Highchart within my Angular 4 application. By utilizing the formatter function of the tooltip, I am able to achieve this functionality. The graphdata array is initially set w ...

The method TranslateModule.forRoot does not require a specific type argument and produces a ModuleWithProviders type

After upgrading my Angular application from version 5 to version 9, I encountered an error during the build process. The error message stated: "TranslateModule.forRoot returns a ModuleWithProviders type without a generic type argument. Please add a ge ...

The child component is receiving null input data from the Angular async pipe, despite the fact that the data is not null in the

I encountered a strange scenario that I'm unable to navigate through and understand how it occurred. So, I created a parent component called SiteComponent. Below is the TypeScript logic: ngOnInit(): void { this.subs.push( this.route.data.subscribe( ...

Combining results from multiple subscriptions in RxJS leads to a TypeScript compiler error

I am utilizing an Angular service that provides a filterObservable. To combine multiple calls, I am using Rx.Observable.zip(). Although it functions as expected, my TypeScript compiler is throwing an error for the method: error TS2346: Supplied paramete ...

Can someone guide me on incorporating static methods into a Mongoose model using TypeScript for Mongoose version 5.11?

When using Mongoose 5.10, I implemented static methods in my Mongoose models with the following pattern: import { Schema, Document, Model, Connection } from "mongoose"; import { v4 } from "uuid"; export interface IFoo extends Document ...

Tips for incorporating Angular-specific code into Monaco Editor

I am currently developing a browser-based IDE using the Monaco editor. I'm allowing users to input Angular-specific code into the editor, like this: import { Component } from '@angular/core'; @Component({ selector: 'app-root', ...

How can I show the localStorage value in an HTML5 template using Angular2?

I have two keys stored in localStorage and I want to display one of them on my template. I'm having trouble accessing these values. I even created an interface specifically for storing the value of the currentUser key from localStorage. How should I g ...

Issue regarding angularjs type definitions

I am facing an issue with installing typings for Angular and I need some guidance on how to resolve the error. Any suggestions or assistance would be greatly appreciated! Below is the error message that I encountered: ERROR in C:\Users\test&b ...

How can I combine my two ngIf conditions into an ngIf else statement?

Having trouble incorporating an *ngIf else with two large <div> elements, as the content seems overwhelming to organize properly while following the documentation. Initially believed that using the same styling for two open text boxes, with one hidd ...

I am encountering an issue with Wedriver.IO where screenshots of executions on a Remote Selenium Grid Hub are not being included in my Allure Reports

wdio.conf.ci.js: The following code snippet has been added. afterTest: function(test, context, { error, result, duration, passed, retries }) { if (passed){ browser.takeScreenshot(); } }, I expect to see a screenshot attachment in the bottom right corn ...