javascriptangular: add 'line break' via string - Stack Overflow

I have this method in my .ts file that creates a string out of obj.date and obj.status.Now I'm us

I have this method in my .ts file that creates a string out of obj.date and obj.status.
Now I'm using multiple(eight, to be exact) \xa0 (a hex encoded nbsp) to make some space between the date and the status.
The resulting string: 17/11/2020       done
is then bound in the template.

I would rather use a line break instead so that it would look like so:
17/11/2020
done

dateWithStatus(obj) {
  return
    moment.utc(object.date).format(DATE_FORMAT)
    + Array(8).fill('\xa0').join('')
    + object.status;
}

My question is - what hex code should I use instead of \xa0 to force the line break?
Neither \n, nor \r\n seem to work.

PS: Yes, I did search the stackoverflow (among other sites) for the answer, to no avail.


=== update === I tested the string literal thingy in the (Angular) template file.


ponent.ts
testString = `abcd
efgh
ijkl`;

ponent.html
<span>{{testString}}</span>

RESULT : all the letters are in one line
abcd efgh ijkl

I have this method in my .ts file that creates a string out of obj.date and obj.status.
Now I'm using multiple(eight, to be exact) \xa0 (a hex encoded nbsp) to make some space between the date and the status.
The resulting string: 17/11/2020       done
is then bound in the template.

I would rather use a line break instead so that it would look like so:
17/11/2020
done

dateWithStatus(obj) {
  return
    moment.utc(object.date).format(DATE_FORMAT)
    + Array(8).fill('\xa0').join('')
    + object.status;
}

My question is - what hex code should I use instead of \xa0 to force the line break?
Neither \n, nor \r\n seem to work.

PS: Yes, I did search the stackoverflow (among other sites) for the answer, to no avail.


=== update === I tested the string literal thingy in the (Angular) template file.


ponent.ts
testString = `abcd
efgh
ijkl`;

ponent.html
<span>{{testString}}</span>

RESULT : all the letters are in one line
abcd efgh ijkl

Share Improve this question edited Feb 4, 2021 at 13:03 pax asked Feb 4, 2021 at 12:22 paxpax 1,9453 gold badges23 silver badges51 bronze badges 3
  • Can't you use template literals (back ticks)? – Roy Commented Feb 4, 2021 at 12:27
  • I did try the back ticks, but the result was the same. All it yielded was one space. – pax Commented Feb 4, 2021 at 12:35
  • All of the answers deal with the <h2> tag that I've chosen only for testing purposes. The tag containing the text in the app is actually <span>, so I changed it in the test example too. – pax Commented Feb 4, 2021 at 13:04
Add a ment  | 

3 Answers 3

Reset to default 6

One of the ways could be adding a class to your element, say, testString, like so:-

.testString{
white-space:pre
}

white-space:pre should allow breaking at \n characters.

You can do it using safeHtml pipe.

// ponent.ts
...
testString = 'Hello<br>world!';
...

// ponent.html
<h2 [innerHtml]="testString | safeHtml"></h2>

You can add a line break in your string and then bind the innerText property of the element:

<h2 [innerText]="testString"></h2>

testString = 'first line' + ' \n ' + ' second line';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744759136a4592065.html

相关推荐

  • javascriptangular: add &#39;line break&#39; via string - Stack Overflow

    I have this method in my .ts file that creates a string out of obj.date and obj.status.Now I'm us

    19小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信