The ngBindHtml directive is failing to display sanitized HTML content

I am encountering an issue with my Angular app where I am trying to insert HTML into the page using ngBindHtml, but it does not seem to be working. Take a look at my code below:

<main id="main" ng-bind-html="oreGen | trust"></main>

Here is my Angular app setup:

angular.module('programApp', ['programApp.controllers','programApp.filters','ngSanitize']);

angular.module('programApp.controllers', [])
  .controller('programController', ['$scope', '$filter', function($scope, $filter){
    $scope.oreGen = '<div class="oreFunction" ng-click="collectFunction(\'parenthesis\', 1)">test text</div>';
    $scope.collectFunction = function(value1, value2){
      alert(value1 + value2);
    };
  }]);

angular.module('programApp.filters', []).filter('trust', ['$sce', function($sce){
  return function(text) {
    return $sce.trustAsHtml(text);
  };
}]);

Upon loading the page, nothing appears within the main element.

Here is the link to the Codepen for reference: http://codepen.io/trueScript/pen/MwbVpO?editors=101

It seems that the div being ngBinded does not display. Any insights on why this might be happening?

Answer №1

If you're looking to enhance functionality, consider utilizing a directive in place of a filter. Take a peek at this JSFiddle example

Snippet of HTML code:

<div ng-app="myApp" ng-controller="programController">
    <dir id="main" content="oreGen"></dir>
</div>

JavaScript snippet:

angular.module('myApp', [])
.controller('programController', ['$scope', '$filter', function ($scope, $filter) {
    $scope.oreGen = '<dir class="oreFunction" ng-click="collectFunction(\'parenthesis\', 1)">test text</dir>';
    $scope.collectFunction = function (value1, value2) {
        alert(value1 + value2);
    };
}])
.directive('dir', function ($compile, $parse) {
    return {
        restrict: 'E',
        link: function (scope, element, attr) {
            scope.$watch(attr.content, function () {
                element.html($parse(attr.content)(scope));
                $compile(element.contents())(scope);
            }, true);
        }
    }
});

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 process for changing colors once vertex colors have been updated?

Explaining the issue with an example. I have included a brief snippet of HTML code here to demonstrate the problem. In this scenario, I have created a simple triangle geometry as a global variable. By clicking the "Red" button, function red() is invoked ...

How can I efficiently include all css from node_modules in vuejs?

For my Vue.js app built with the webpack template and vuetify, I am starting by importing the vuetify css in my App.vue. <style> @import '../node_modules/vuetify/dist/vuetify.min.css' </style> I'm wondering if this is the c ...

Transform the array of objects into different clusters

I am facing a challenge where I have an object returning two arrays of objects from an API. My goal is to transform this data into a new array of objects in order to effectively group the information for the Vue v-select component. For a visual representat ...

What causes a ReactJS component to disappear upon page refresh?

After the user clicks the Login button in the Instructor Login Form, I want to display the Instructor Profile component. Everything functions properly until I refresh the page, at which point the User Profile component disappears. Here is a screenshot of ...

Leveraging Multiple MongoDB Databases in Meteor.js

Can 2 Meteor.Collections fetch data from separate MongoDB database servers? Dogs = Meteor.Collection('dogs') // mongodb://192.168.1.123:27017/dogs Cats = Meteor.Collection('cats') // mongodb://192.168.1.124:27017/cats ...

How to deactivate the mobile hardware back button functionality in Ionic 2

Our team has been developing a business application and we've encountered a perplexing issue. Every time we press the mobile hardware back button, our application's GUI becomes disrupted. Despite dedicating numerous hours to finding a solution, t ...

React fails to display the content

Starting my React journey with Webpack configuration. Followed all steps meticulously. No error messages in console or browser, but the desired h1 element is not showing up. Aware that React version is 18, yet working with version 17. App.jsx import Reac ...

Unveiling the approach to accessing a nested function with jQuery

While the title may be a bit misleading, I couldn't think of a better way to describe it. I've created a function that allows a small pop-up window to appear when a link is clicked (to confirm whether or not an article should be deleted). Addit ...

ReactJS does not update the conditional CSS class when hovering with mouseOnEnter or mouseOnOver

I am currently attempting to showcase data in a table where each row features an info icon that is only visible upon hovering. Additionally, I want a pop-up to appear when the info icon is clicked (there is an onclick function assigned to this button). He ...

Having trouble retrieving data from the table with AJAX and CodeIgniter

I am currently developing a comprehensive HRM+CRM system (Human Resource Management and Customer Relation Management). I have encountered an issue while trying to generate an invoice for each customer. I am struggling to resolve this problem and would appr ...

Tips for customizing the appearance of grouped rows in a Vuetify datatable with the use of slots

Has anyone successfully utilized grouped rows with v-slot in the most recent Vuetify versions? The example they provide is as follows: <template> <v-data-table :headers="headers" :items="desserts" item-key="name" group-by="categ ...

The frisbyjs test is not passing due to the absence of proper HTTP headers being sent by the get()

My frisbyjs test is failing because the x-access-token and x-key HTTP headers are not being sent. Am I missing something? This seems like a simple mistake. Here is the outer test that contains the failing test within afterJSON(): frisby.create('Logi ...

What methods are available to verify the local installation of an NPM package that I have released?

After successfully releasing a new package on NPM, I encountered an issue. While the package performs flawlessly on my computer, errors pop up when my coworker tries to install it using 'npm install'. What is the optimal method for installing an ...

How to sequentially load multiple obj files in Three.js

Currently, I am utilizing the obj/mtl loader from three.js to import various obj files along with mtl files. The challenge I am facing now is the need to load multiple objs one by one. Even though I have implemented THREE.DefaultLoadingManager.onProgress t ...

Exploring the DOM, store all anchor elements inside a <ul> tag with a specific ID in an array

I'm attempting to extract all the anchor tags from a list and store them in an array by traversing the DOM. So far, I have been successful in getting the list items and their inner HTML into an array, but I am facing difficulties in retrieving each LI ...

The tubular.js Youtube video background is overlapping my other components on the site, instead of displaying behind them as intended

I recently implemented the tubular.js script on my webpage to display a YouTube video as the background. On the tubular page, there is a statement that reads: First, it assumes you have a single wrapper element under the body tag that envelops all of ...

How can I color the first, second, and third buttons when the third button is clicked? And how can I color all the buttons when the fourth button is clicked?

I am trying to achieve a task with 4 buttons. When the third button is clicked, I want the first, second, and third buttons to change color. Similarly, when the fourth button is clicked, I want all buttons to change color. Additionally, I need to save a va ...

Obtain a string with commas separating the elements of an array

In my JavaScript code, I am working with the following JSON data. { "url":"http://example.com/main.aspx", "report_template_id":"1", "interval_secs":"86400", "analysis_type":"lite", "email":"<a href="/cdn-cgi/l/email-protection" class="__ ...

Exploring the integration of angular-ui-select into an angular seed project

I set up a new project using the starter template from https://github.com/angular/angular-seed and now I'm attempting to integrate angular-ui-select for dropdown menus. I've added select.js and select.css files to my index.html as well as install ...

What is the process for setting up a system where the database alerts the server, which then sends out new data to the client application

I am currently working on a betting application where coefficients are constantly changing. The current setup involves the application making an http request every 5 minutes, but I see some issues with this approach. There may be times when no changes oc ...