The ngBindHtml feature in AngularJS 1.2 does not handle processing of line breaks (/r & /n)

I have a string with a lot of extra whitespace, as it appears on my server:

 var care_description = "MATERIAL\r\n    \r\n  56% Acrylic, 24% Rayon, 20% Polyester\r\n \r\n  CARE\r\n  \r\n  Machine Wash, Gentle Or Delicate"

Using the new Angular 1.2.0 ngBindHtml, I'm processing it in my controller like this:

    $scope.care = $sce.trustAsHtml(care_description);

(the $scope.records[i].accordions array is just a wrapper for Angular-Bootstrap's Accordions module).

However, when I display it in my view (using

<p ng-bind-html="care"></p>
), it shows up as:

 MATERIAL 56% Acrylic, 24% Rayon, 20% Polyester CARE Machine Wash, Gentle Or Delicate

When it should actually be:

MATERIAL

56% Acrylic, 24% Rayon, 20% Polyester 

CARE 

Machine Wash, Gentle Or Delicate

One possible solution is to use a regex replace to find all instances of \r\n and replace them with <br />.

Edit: It's worth noting that even though there are no HTML tags in the example above, there are often some, which is why I need to use ngBindHtml instead of <pre> tags.

Answer №1

NgBindHtml simply prevents tag escaping. In this case, there are no tags present. An alternative solution could be replacing all instances of \r\n with <br/>. Alternatively, you could create a custom directive for this task, or utilize <pre> tags.

Answer №2

That code snippet you're working with isn't actually formatted in HTML; it's just plain text. As a result, there's no need to utilize ng-bind-html or trustAsHtml. Instead, you can simply showcase the value within a <pre> block:

<pre>{{ message }}</pre>

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

Is GetStaticPath in NextJS suitable for generating large amounts of static data?

On my website, I have a page dedicated to displaying information about hotels based on their unique IDs. To achieve this functionality, I utilize getStaticPath to generate paths like /hotel-info/542711, where 542711 represents the hotel's ID. However ...

Which is more recommended to use in AJAX (XMLHttpRequest) - eventListener or readyStateChange method?

As I revisited a video from WWDC12 discussing advanced effects with HTML5, I couldn't help but notice that for the demo they utilized req.addEventListener("load",callback,true) instead of the usual onreadystatechange. This made me wonder: what differ ...

Unable to receive Ajax POST requests in PHP

I've spent all day searching for a solution to my problem... I have an ajax post method that sends a variable value to a php page. The ajax post seems to be working fine as indicated by the success message, but I am unable to retrieve the value in php ...

Encountering difficulty locating a module despite following the correct path according to the official NEXT.js documentation

Recently, I delved into the world of next.js and found myself engrossed in chapter 4 of the official documentation titled "Creating Layouts and Pages." In this chapter, I was prompted to create a file named layout.tsx and insert the following code: import ...

Anticipating the resolution of the $rootScope value in Angular before the page is fully loaded

I've encountered an issue while using ngView with a static navigation bar that persists throughout the application: <div ng-include="'views/nav.html'" ng-controller="NavCtrl"></div> <div class="container-fluid" ng-view=""> ...

What are the benefits of using React.useMemo or React.useCallback within component props?

Exploring efficient ways to implement TailwindCSS in React, considering its utility-first nature leading to component-heavy code (e.g. className="w-full bg-red-500"). One approach is creating a utility function like: utils/tailwind.ts const tw = (...clas ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

How can I retrieve data from local storage based on a specific key value using a query?

In order to optimize the storage of data for my app, I have successfully stored a large amount in local storage. Now, I am faced with the task of fetching data in an array but only selecting key/values that are specifically chosen, rather than all or jus ...

The process of combining identical data values within an array of objects

Can anyone help me with merging arrays in JavaScript? Here is an example array: [{ "team": team1, "groupname": "group1", "emp-data": [{ "id": 1, "name": "name1", }], }, { "team": team1, "groupname": "group1", " ...

Using JavaScript to replace a radio button with the term "selected"

I am currently in the process of developing a quiz that is powered by jQuery and possibly JSON, with data being stored in a database. Everything is functioning correctly at this point, but I would like to enhance the user interface by hiding the radio butt ...

Enhancing Angular input validators with updates

Working on a project with Angular 6, I have set up an input field using mat-input from the Angular Material framework and assigned it an id for FormGroup validation. However, when I initialize my TypeScript class and update the input value, the validator d ...

Adding External JS Files to a Node.js Project

I recently discovered how to incorporate a JS file into Node JS. However, I am facing an issue with two JS files in particular - hashing.js and encoding.js. Within encoding.js, I have the following code: exports = exports || {}; exports.encoding = encodi ...

Exploring Angular Material Design's method for vertically populating a column

Is there a way to fill a column vertically in my current app? https://i.sstatic.net/uu4yO.png I've been struggling for hours trying to make the vertical items span the entire height of the page. I have pored over documentation and other posts, but I ...

Using Vue.js to dynamically change attribute values based on a condition

I am currently displaying a v-tippy in the following manner: <label v-tippy content="My content text"> Everything is functioning as expected, but now I want to display it based on a condition show_tip == true. Is there a way to achieve th ...

Clicking on a button in HTML to open a JavaScript file

As a beginner in HTML and JavaScript, I am looking for a way to open a JavaScript file when the onclick event of a button in my HTML file is triggered. Can anyone provide me with guidance on how to achieve this? Thank you in advance. ...

Snapjs allows you to utilize the same drawer for both the left and right sides

I typically use the same drawer for both left and right, depending on the language: <div snap-drawer="{{$root.direction=='rtl'?'right':'left'}}"> The issue arises when the direction is changed in real-time and the swip ...

Using React to Render a Component with Local Storage Information

I'm in the process of developing a history list component for a form within a react application and encountering difficulties with local storage. The goal is to display a list of previous user inputs from the local storage data. My current approach i ...

Experimenting with animating an element through onClick event using JavaScript

I would like to create an animated div using JavaScript that activates on click. <div class="Designs"> <p>Designs</p> <div class="Thumbnails" data-animation="animated pulse"> <a href=" ...

What is the best way to ensure that the user interface stays updated with alterations made to

I am currently studying various front end design patterns, and I repeatedly come across the idea of implementing a virtual shopping cart in a shopping cart scenario. The suggestion is to have the user interface actively monitor any changes to the cart and ...

What is the best way to implement media queries for mobile phones and desktop computers?

I've come across similar questions before but still can't wrap my head around it. Here's my dilemma: I want the index page of my website to display in desktop layout on desktops and mobile jquery on mobile devices. Currently, I have set up m ...