AngularJS secureHTML verify image click event

Just starting out with AngularJS and hoping for some assistance :)

I have a div that has the details of an activity, formatted in HTML. To display this content correctly, I am using trustAsHTML:

<div class="description-div">
  <div ng-bind-html="renderHtml(booking.description)">
  </div>
</div>

There is a $rootScope function involved:

$rootScope.renderHtml = function (htmlCode) {
return $sce.trustAsHtml(htmlCode);
};

The description may include images, so I would like to trigger a popup when an image is clicked.

Any suggestions on how to achieve this? I believe a directive might be needed, but my attempts so far have been unsuccessful.

Appreciate any guidance you can provide!

Answer №1

Avoid polluting $rootScope with unnecessary helper functions to prevent it from becoming overly complex.

One effective method to achieve your goal is by using a directive:

app.directive("myBooking",["$sce", function($sce) {
  return {
    restrict: 'E',
    scope: {
      booking: "="
    },
    template: "<div ng-click='click($event)' ng-bind-html='trustedHtml'></div>",
    link: function(scope, elem, attrs) {
      scope.trustedHtml = $sce.trustAsHtml(scope.booking.description);
      scope.click = function($event) {
        if($event.srcElement.tagName.toLowerCase() === 'img') {
           // Perform specific action for the desired image...
        }
      }
    }
  };
}]);

If the booking object belongs to a controller's property in its scope, utilize the directive as follows:

<my-booking booking="booking"></my-booking>

The directive mentioned assumes that the img click event will bubble up, meaning that the trusted HTML does not intercept img click events. Here is a link to a plunkr example showcasing this directive in action.

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

Updating the CSS: Using jQuery to modify the display property to none

I am facing an issue with displaying an element that is defined as display:none in CSS. I tried to use the .show() function in jQuery, but it's not working as expected. Here's the code snippet: CSS .element { position: absolute; display: no ...

I am having an issue with my registration form in node.js where it does not redirect after submitting

I am currently working on implementing a registration form using node/express for my website. The routes are all set up and the database connection is established. However, I am encountering some issues when users try to submit the registration form on th ...

JSfiddle not loading properly on website

I am facing an issue with my Jsfiddle code. It works correctly there, but when I copy it into my webpage along with the CSS and JavaScript files, it doesn't work. Can anyone provide insight on how to properly transfer the code to display it correctly ...

How can we make the lines in the Highcharts force plot take up all the available width

Our design team has specifically requested that our stacked area charts fill 100% of the chart width, instead of stopping at the tick marks. To illustrate this, they have provided a mockup: View Mockup Currently, our chart does not extend to the full wid ...

The size of my React Native app is significantly larger than expected once installed

I recently developed a React Native app, and while the release APK size is only 28 MBs, I was shocked to see that the storage size is a whopping 62 MBs. I am desperately looking for a solution as I need to deliver this project soon. Please help me resolv ...

Ensuring the consistency of form fields using AngularJS

Using Angular 1.5.11 I am currently working on an HTML form that contains multiple fields, such as : <div ng-app="validationApp" ng-controller="mainController"> <div class="container"> <div class="ro ...

Having trouble loading HTML content from another file

Here is the code snippet in question: <script> var load = function load_home(){ document.getElementById("content").innerHTML='<object type="type/html" data="/linker/templates/button.html" ></object>'; } </script> ...

Utilizing Vue JS to showcase a pop-up block when hovering over a specific image

There are four images displayed, and when you hover over each one, different content appears. For example, hovering over the first image reveals a green block, while hovering over the second image reveals a blue block. However, the current logic in place i ...

When utilizing KineticJS on a canvas that has been rotated with css3, the functionality of the events appears to be malfunctioning

Currently, I'm working on a rotating pie-chart widget using Kineticjs. However, I have run into an issue where the events don't seem to function correctly when drawing on a rotated canvas element (with the parent node being rotated 60deg using CS ...

Unexplained disappearance of div element in Vue Router's Navbar

I have been working on integrating a Vue Router into my navbar and everything seemed to be going well. You can check out the code here. The navigation menu I created works perfectly, allowing users to navigate between the home template and about template w ...

The jQuery UI Dialog is experiencing an issue with a button that is triggering a HierarchyRequest

I am facing an issue with a piece of javascript that works perfectly on other pages but is now throwing a HierarchyRequestError on a new page. This leads me to believe that there may be an HTML problem on this particular page. Here is a simplified version ...

An error occurred while trying to import a module due to an unexpected token

Take a look at this codepen link I encountered an error (line 10 in index.vue) with the following import: import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js"; Any idea what could be causing this issue? All other ...

Modify the behavior of the Android back button when a Popup is displayed in Ionic

I'm attempting to accomplish the following: Scenario 1 User presses the back button. A popup appears asking if the user wants to exit * User presses the back button. The app exits * and Scenario 2 User presses the back button. A popup ...

Using a loop variable within a callback function in JavaScript/TypeScript: Tips and tricks

I have a method in my TypeScript file that looks like this: getInitialBatches() { var i = 0; for (var dto of this.transferDTO.stockMovesDTOs) { i++; this.queryResourceService .getBatchIdUsingGET(this.batchParams) ...

Restore checkbox to default setting

Is it possible to reset checkboxes in a form back to their initial status using javascript, PHP, jQuery, or any other method? Here is the code I am currently using: <form method="POST> <input type="text" name="name" id="name" value="default val ...

Getting rid of the scrollbar in Internet Explorer

Instead of just removing the scrollbar, I want to ensure that scrolling capabilities remain intact. This is important because I want to create a 'slide show' effect on the website where users can click 'next' and scroll through content ...

grid causing images to display incorrectly in incorrect positions

My project consists of 3 component files and a CSS file, but I am facing an issue where the Tiles are slightly off in their positioning towards the top left corner. Although no errors are being thrown, upon using the view grid feature in Firefox, it is ev ...

I'm curious as to why styled components weren't working before

I'm attempting to utilize the before on styled components in React, but it doesn't seem to be functioning correctly. Prior to adding before, the background image was displayed, but after its inclusion, the image is no longer showing up; import st ...

Trouble accessing onclick function

My dataSend AJAX function is not being called when I use the onclick event. I have checked in the Inspector of my browser and confirmed that the click handler is attached to it. However, when I set a breakpoint in the function using the Debugger, it never ...

Is it possible to scroll by using the dragenter event?

Looking for a way to achieve incremental scroll up and scroll down using jQuery without jQuery UI? Here's the scenario - I have two divs: <div class="upper" style="height:35px;background-color:red;right:0;left:0;top:0;position:fixed;width:100%;z-i ...