Saving HTML content from a textarea using AngularJS

Here is the custom HTML I am working with:

      <div ng-if="!item.edit>
           <div class='door-description' ng-bind-html="item.Description"></div>
           <div class='door-softvalues' ng-bind-html="item.SoftValues" ng-if="item.IsConfiguration"></div>
      </div>



<div ng-if="item.edit">
    <textarea elastic class='door-description' ng-bind-html="item.Description" ng-model="item.Description"></textarea>
    <textarea elastic class='door-softvalues' ng-bind-html="item.SoftValues" ng-if="item.IsConfiguration" ng-model="item.SoftValues"></textarea>
</div>

Issue arises when line breaks are lost after saving and retrieving content in non-edit mode.

View during editing:

https://i.sstatic.net/FXWW3.png

View without editing:

https://i.sstatic.net/84vBo.png

Even toggling between edit and non-edit modes shows the same result.

What can be done to ensure that line breaks are maintained in non-edit mode?

Answer №1

Have you considered using the pre tag in your code:

<pre class='door-info' ng-bind-html="item.Description"></pre>

Answer №2

By simply adding the following CSS style to your element, you can achieve the desired outcome:

white-space: pre;

Answer №3

Could you try implementing it this way?

 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-sanitize.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<textarea elastic class='door-description' ng-bind-html="myText " ng-model="myText "></textarea>
</div>

<script>
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl", function($scope) {
    $scope.myText = "My name is: <div><h1>John Doe</h1></div>";
});
</script>

Give it a try and see if it works for you.

Once implemented, it should display:

My name is: <div><h1>John Doe</h1></div>

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

Avoid the scenario in which JQuery .HTML replaces the existing .HTML content before it is shown

In my current implementation, I am using a multidimensional array to store values and loop through it based on specific criteria. This process involves determining if a number matches and if it has a set status, such as "487 Online." The data in the array ...

I am experiencing an issue with my AngularJS mock where it is not providing any data

I have created a service and am now working on creating basic test coverage for it. Below is the code for my service: App.factory('encounterService', function ($resource, $rootScope) { return { encounters: [], encountersTotalCount: 0, ...

Resolving issues with JavaScript caused by Polymer updates

I am a novice when it comes to working with Polymer. From what I have gathered, there seems to be compatibility issues with Mozilla and Safari. After researching on StackOverflow, I found that adding addEventListener('WebComponentsReady', funct ...

The ng-blur functionality will only trigger if the click event occurs within the bounds of the textbox

I am facing an issue with a textbox that should display a certain value even after being deleted. When the input is cleared and clicked away from, I want the value to reappear in the textbox. Currently, there seems to be a problem with this functionality ...

An error occurred in NextJS because the property 'title' is being read from an undefined value, resulting in a

I've encountered a problem while trying to deploy my blog app to production. Everything was working fine in the development environment, but now I'm facing this error: Error occurred prerendering page "/blogs/[slug]". Read more: https ...

The onClick event function is activated when the component is first rendered on the screen

Below is the function in my component: myFunc = () => {...} This is how I implemented it: <MyComponent onClick={this.myFunc()}/> The onClick function will trigger when the component mounts. However, if I use either of these alternatives: < ...

I'm looking to fill multiple input boxes with the same value

I am looking to pass the same value to multiple input boxes. Currently, the script below only displays in one box but I want to display the main box result in all multiple input boxes. <script type='text/javascript' src='http://code.jque ...

Res.redirect() function does not redirect the browser URL as expected when triggered by a request made through a frontend fetch() call

Encountering a new issue that is challenging me. In the backend, there is an API route that redirects the browser URL to /signin using res.redirect('/signin'). Upon this redirection, React Router triggers the rendering of a 'log back in&apos ...

Is it a graphics card malfunction or a coding complication? Delving into THREE.JS WebGL

Recently, I created a cool scene using three.js that was running perfectly until today. Strangely, without any changes to the code, I started encountering an error in every major browser - Chrome, Firefox, and Edge. The error message I am seeing is: THREE. ...

Is it possible to prevent the selected option from being available in other select lists using AngularJS?

Can anyone help me figure out how to disable an option in one select list when it is selected in another using AngularJS? I have set up a select list for From Year and To Year with ng-repeat, which is working fine. Now, I just need to implement the functio ...

Is it possible to trigger a script tag based on certain conditions using a JavaScript function?

I'm currently working with Angular and have a requirement to trigger a third party script from a function located within another script tag. Here's an example scenario: Within the index.html file let displayOnHomepage = () => { if (win ...

Why isn't my computed property functioning properly in Vue.js?

Exploring the code snippet provided below: new Vue({ el: '#app', computed: { myMessage: function() { return "hello"; } }, data: { message: this.myMessage }, mounted: function() { console.log(this.myMessage); ...

Optimal technique for adding elements to the DOM using ngFor

My application features a websocket connected to an ngFor loop that populates data from approximately 100 records. Each entry in the list has a button with a click event attached, triggering the creation of a loading spinner via a 'div' element w ...

Ways to prevent memory leaks in Angular applications

I have been searching for answers on whether my AngularJS code is causing a memory leak, but so far I haven't found any conclusive information. While I've come across articles discussing JavaScript memory leaks, they don't seem to address t ...

Counting the number of PHP inputs in a field

Hello, I am using a PHP script from Steve Dawson's website. To display the output on my HTML page, I am utilizing this AJAX script: <script> $.ajax({ type:'GET', url:'http://www.solariserat.se/count.php', data: ...

Creating a MultichoiceField with ModelForm in Django to handle database relations

Hello everyone, this is my debut post on Stackoverflow so please bear with any errors. I've exhausted several solutions before turning to the community for help. My issue involves creating a ModelForm that allows customers to select multiple users for ...

Reversing data in Vue3

I've been struggling to create a custom dropdown for my application. I need to implement a function that adds a class called is-active to a div element. So, what I have done is created a simple div with an onclick function as shown below: <div :cla ...

Unable to show <LI> elements

I'm having trouble adding LI items to the #historial <UL> tag. Whenever the for loop is inside the function, the list displays with some elements duplicated. I believe the for loop should remain outside of the function. Could someone please he ...

Styling buttons with different colors using React onClick event.getSelectedItem() method

I am having an issue with adding a border when the class is set to "transportation active" in my React code. When I click on one of the icons, all of them become active instead of just the clicked one. This behavior is not what I want, as I only want the ...

Creating an AngularJS directive that utilizes a switch statement based on the value of an attribute

I have a specific directive that is assigned one of 13 different types as an attribute. Based on the type, the directive will render various HTML elements corresponding to that type. The type value remains constant and does not change once the directive is ...