AngularJS Textarea with New Lines after Comma

As a beginner in front-end development, I am exploring different approaches to handling a text area that inserts a new line after each comma. My main focus is on finding the best solution within AngularJs (filter, Regex, etc).

Before:

hello,how,are,you

After:

Hello
how
are
you

Current Setup:

<textarea cols="105" rows="30" disabled="true"
          style="background-color: floralwhite; resize: none; white-space: normal; margin-top: 0px; border: none;">
    {{profile.exampleValues}}
</textarea>

I attempted to create a filter but did not succeed:

.filter('newlines', function () {
     return function (text) {
         return text.replace(',', '<br/>');
     }
})

Answer №1

Your code has been updated and fixed successfully.

One change that was made is replacing white-space: normal; with white-space: pre-line;

Additionally, the filter has been enhanced to handle a regular expression. According to information from W3C:

The replace() method searches for a specified value or regular expression within a string, then replaces it with another value.

Note: When replacing a specific value (not using a regular expression), only the first instance will be replaced. To replace all occurrences of the value, add the global (g) modifier.

app.filter('newlines', function () {
  return function (text) {
    return text.replace(/,/g, '\n');
  };
});

After making these changes, the textarea should appear as follows:

<textarea cols="105" rows="30" disabled="true"
          style="background-color: floralwhite; white-space: pre-line; resize: none; margin-top: 0px; border: none;">
    {{profile.exampleValues | newlines}}
</textarea>

Please refer to this Plunker link for further details.

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

Utilizing AngularJS: Techniques for Binding Data from Dynamic URLs

Just starting out with AngularJS We are using REST APIs to access our data /shop/2/mum This URL returns JSON which can be bound like so: function ec2controller($scope, $http){ $http.get("/shop/2/mum") .success(function(data){ ...

Crafting a responsible table design with the help of grid layout techniques

Is there a way to create a table that can adjust its rows and columns based on the container width or when resized? https://i.sstatic.net/xeZjl.png https://i.sstatic.net/kdpJC.png I attempted to use grid-gap in the background, but it resulted in empty red ...

"Implementing a dynamic way to assign values to different item types in React

There is an object with multiple values inside: const [sort, setSort] = useState({ "city": [], "price": [], "year": [] }); When the "add" button is clicked, the "city" value should be updated to include certain va ...

AngularJS - returning dynamic functions

index.html <div ng-repeat="object in objects" ng-style="getStyle(object.id)"></div> controller.js [...] $scope.startWidth = 100; $scope.getObject = [...] $scope.getStyle = function(id) { var widthVal = $scope.startWidth; $scope.star ...

The functionality of ng-repeat does not support multidimensional arrays

I am facing an issue with my code involving ng-repeat and an array. The ng-repeat directive is not working as expected on this particular array, even though it works fine for other scopes. I have gone through the code multiple times but still can't se ...

What is the best way to generate a list from a text file in javascript?

I have a document called department.txt which lists various departments: Chemistry Physics Mathematics Other I'm looking to generate a dropdown menu <select> in my HTML by importing this file. How can I achieve this using Javascript? There are ...

Looking to update the background color of the items in a sliding door menu using CSS

I'm having trouble changing the background color of my list items in a sliding door menu using CSS. I've successfully changed the left border to green and the text to white, but the background color remains unchanged. Below is the code I'm u ...

How to target the following sibling element (not a child) with CSS

I am working with the following HTML structure: <div class="col-sm-12"> <input id="my_id"> <div class="col-sm-1 col-3-box" style="position:absolute; margin-left:340px;"> </div> </div> How can I target and change ...

Add a parameter within a loop using JavaScript

I am currently working on a function with a parameter called menu. removeChildCheck:function(menu){ let removeArrayValues = []; for(var i=0; i < this.checkbox.menu.length; i++){ removeArrayValues.push(this.checkbox.menu[i].value); ...

Column wrapping in Bootstrap is a powerful feature that allows for

Looking for a layout similar to this: https://i.sstatic.net/BTuED.png Need a responsive design using Bootstrap 3 The purple block (col-sm-7) with a fixed height, but it can be optional The blue block (col-sm-5) with a variable height (not dynamic), and ...

I'm looking to position the vibrant red box at the bottom of the screen, right under the navigation bar. However, every time I try to

I'm struggling to align the full-screen red box under the navigation bar. I've tried using flex properties but it's not working as expected. My goal is to have the red box at 100% width and be responsive. Could it be something with my nav b ...

I'm looking for a way to create a Redux thunk action creator that will return a promise. How

In my code, I have a primary thunk that is triggered by a button click. Within this thunk, I need to invoke another thunk and ensure it completes before proceeding. The second thunk returns a promise. Below is an excerpt of the code in question: export f ...

How to Accurately Obtain Serial Numbers in Add, Remove, and Clone Operations Using My Calculation Method

How To Properly Calculate SrNo in the Add, Remove, and Clone Functions By clicking "Add" multiple times, deleting rows, and then adding again, the SrNo becomes incorrect. I want it to calculate properly with my custom function... <div id="button_pro" ...

Update app content without requiring users to download a new APK

Hey there, I'm new to all of this so I appreciate your patience. I'm working on a video based app and I'm wondering how I can add new videos/content to the app without having users download a new apk. I know I need to host the app on a data ...

Utilizing React portals for displaying a modal component upon clicking a table row

As I familiarize myself with React, the concept of portals in the developer documentation caught my attention. However, I am struggling to grasp how this portal component renders on-demand and how data can be passed to it to populate a modal. My current s ...

Padding provides varying padding for the top and bottom sections

html { font-size : 10px; } body { text-align : center; color : #9da8c1; font-family : sans-serif; background : #013f81; } h1 { font-size: 4rem; } #img-div { background : #2a5590; width : 600px; margin : 0 auto; pad ...

Issue with NodeJS SQL Login: Received error (ERR_HTTP_HEADERS_SENT): Headers cannot be set after being sent to the client

Hi there, I am fairly new to working with nodeJS and I have encountered an issue that I suspect lies within the second if statement inside "db.query..." in the code provided below. An error message showing ERR_HTTP_HEADERS_SENT]: Cannot set headers after ...

Enigmatic void appears above row upon removal of content from a single item

When I click on an item in my grid, the content of that item is moved to a modal. The modal functions properly, but I noticed that when the content is removed from the item, a space appears above it. I have found that using flexbox could solve this issue, ...

New to JavaScript and Bootstrap - eager to learn by using Bootstrap 4 Chart Template, just need help with a small issue in the code

I am eager to dive into Bootstrap 4 and data visualization charts. I want to replicate the visualizations found in the link below. However, after copying and pasting the code into Atom, it's not functioning as expected. I ensured that I copied the HT ...

Retrieve the date information from the <td> element, if it exists

There are numerous rows in the table. The table cell labeled "dates" may contain either a specific date or the word "Permanent". If there is a date present and it happens to be greater than today's date, it needs to be highlighted in red. I attempted ...