Is it possible to set up ng-bind-html so that it displays a string such as '</'?

When using ng-bind-html, the issue arises where it interprets '

' as a closing tag. This results in the text being sanitized and passed through, but when rendered in HTML, it appears as if nothing is displayed beyond the closing tag. For instance, 'I find <code></
is a good thing' only shows 'I find' on the webpage after binding the string.

(additional information) The root of the problem lies in the conflicting usage of html encoding and url encoding simultaneously. In our current setup, the input from the frontend looks like '</ is good'. This input undergoes encoding via a service, transforming it into '&lt%2F is good', then ultimately becoming '&lt/ is good' before insertion into the database. Upon retrieval, ng-bind-html interprets this as '</ is good'. The issue is further complicated by an input field with ng-model that receives this data as '$lt/ is good', mirroring the database content. To address this inconsistency, a replace() function is implemented in the controller before editing. While this serves as a temporary fix, uncertainties remain regarding its correctness. Even platforms like StackOverflow face similar challenges, evident in the grey background applied to '</' due to the rich text editor interpreting it differently. As a workaround, backtick escapes are utilized for this particular string within the text editor display.

Answer №1

By changing ''<'' to ''&lt;'', the expected results are achieved:

angular.module("app",[])
.controller("ctrl", function($scope,$sce) {
    $scope.text = "I find &lt;/ is a good thing";
    $scope.trust = $sce.trustAsHtml($scope.text);
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
   <h2>Using ng-bind-html</h2>
   <p ng-bind-html="trust"></p>
   <h2>Using ng-bind</h2>
   <p ng-bind="text"></p>
</body>

For further details, please visit

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 most efficient way to transform an array of objects into a compact string?

I'm exploring a new project with an import/export feature in mind. Initially, I will have an array containing approximately 45 simple objects structured like this: {"id": "someId", "quantity": 3} To make it exportable, t ...

Strategies for disabling middle mouse button default behavior in Vue

When I use @click.middle.stop.prevent="test", the default scroll wheel still shows up despite it detecting the middle mouse click and calling my function. Is there a way to prevent this from happening? ...

Combining the devexpress dxDataGrid with Angular's $scope for seamless web development

I'm encountering difficulties with binding $scope in angular and dxDataGrid. Utilizing the devexpress library dx.all.js, which enhances the dxDataGrid with various features, I have a div for dx-data-grid and attempting to transfer the selected row da ...

Converting PHP array into a JavaScript object array

I have a database table with three columns labeled as prop_no, prop_name, and prop_sc. The data from the last two columns has been extracted into two distinct PHP arrays named $propertyNameList and $propertyCodeList. I am now facing the task of transferrin ...

How come I am getting the desired section from my split string only after running the function twice?

I've been working on a function that retrieves data from a form and sends it to a POST request, which in turn sends it to my MongoDB database. The following code snippet showcases the process of uploading an image to IPFS, extracting the IPFS hash fro ...

Unlocking the secret dropdown menu with Java Selenium WebDriver

Is there a way to retrieve a hidden dropdown menu on a webpage using selenium webdriver? The process involves selecting an option from a navigation bar, which triggers the appearance of the dropdown menu. I then need to choose a value from the list on the ...

The multi-level dropdown feature in the BootStrap-4 navbar extends off the screen when the dropdown menu is opened

I have been struggling to make this work. I used BS4 for responsiveness, and my navigation items are aligned to the right in the navbar. Despite trying various methods to position the submenu on the left of the parent menu, I still can't get it to dis ...

Angular Cross-Origin Resource Sharing problem: handling 'Access-Control-Allow-Origin' issue

Here is the configuration for my CORS filter on Tomcat 7.0.59: <filter> <filter-name>CorsFilter</filter-name> <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> <init-param> < ...

Trouble with Angular 1.6 ng-repeat not showing search results

I can't seem to figure out why ng-repeat is not showing the search results. Here's what works: Using an HTTP GET request to retrieve all data from the database, and seeing the results displayed by ng-repeat. Sending an HTTP GET request with ...

Control checkbox state based on Grandparent or parent selection in Vue

Currently, I am attempting to toggle a checkbox based on the status of either the grandparent or parent checkboxes: new Vue({ el: '.app', data: { grand_parent: false, parent: false } }) Unfortunately, it is not working as expected ...

Continuously monitor the condition in my function

I'm encountering an issue with my jQuery function where it animates a progress bar from 0 to 100 when it's visible on the screen. The problem arises when the progress bar is not initially visible upon page load, as the animation will never trigge ...

Conditional statements in jQuery for identifying a specific div based on its id

My current setup involves an HTML table that gets populated based on a mysql query. The table is enclosed within: <div id="punchclock" class="timecard"> I also have a jQuery script in place to calculate the totals of the times entered into this tab ...

Troubleshooting Azure SDK: Tips for Displaying the Appropriate REST API

Currently, I am utilizing the Azure JS SDK to interact with Postgres in Azure using various modules such as @azure/arm-postgresql, @azure/identity, and @azure/functions. My main goal is to access the underlying REST Api for debugging purposes. For instanc ...

Select the correct nested div with the same name by clicking on it

My problem involves nested div elements with the same class. For example, I have a Panel inside another Panel. However, when I click on the inner panel, it is actually the outer panel that triggers the $(".panel").click function. What I need is for the ...

Generating random indexes for the Answer button to render

How can we ensure that the correct button within the Question component is not always rendered first among the mapped incorrect buttons for each question? Is there a way to randomize the positions of both correct and incorrect answers when displaying them, ...

Customizing error messages in react-hook-form-mui: A step-by-step guide

I'm currently diving into the world of react-hook-form-mui library. The documentation provided for this project is quite brief, and I've been struggling to add a validation rule to my form field. Despite trying various methods, none seem to be ef ...

Establishing connectivity between an AngularJS application and Mongodb by utilizing NodeJS

I have implemented an AngularJS application that successfully retrieves data from an array in a controller using ng-repeat. Now, my goal is to integrate it with Mongodb through NodeJS so that the app can fetch data from a Mongodb collection. Additionally, ...

The perplexity of ES6 Promises

Within an asynchronous function, I encountered the following code snippet: await application.save((err) => { console.log("hello"); if (err) { res.status(500).send({ message: "Error encountered" }); } }); conso ...

Is it ever appropriate to make a service available at the root level?

While I am aware that global variables are typically considered an anti-pattern, especially in the context of Angular development, I have encountered a scenario where I am contemplating breaking this rule. After reading through the comments and acknowledg ...

Using jQuery or JavaScript to delay execution until the image width has been adjusted

I'm encountering a timing issue with the loading functions on my page. The problem arises when I need to retrieve the width of an image after two specific events have occurred: 1) The image has finished loading 2) The image's width has been adj ...