22

UPDATE: Both ways from Chris and Mark work.

I am using Angular 2. I try to show two continuous spaces between a and bin the page. I've tried all of these, but none of them works:

{{text}}

text = "a\00a0\00a0b";
text = `a\00a0\00a0b`;
text = "a  b";
text = `a  b`;
text = "a  b";
text = `a  b`;

How can I make it work?

0

3 Answers 3

27

Bind to DOM property innerHTML instead of DOM property textContent (which is what {{}} binds to):

<span [innerHTML]="text"></span>

text = "a&nbsp;&nbsp;b";
Sign up to request clarification or add additional context in comments.

1 Comment

In version 6.0.0 I found that you have to add single quotes around the text string since it otherwise assumes the whole thing is a variable. You also can't take the square brackets from around innerHTML as that complains for a different reason. So basically text = "'a&nbsp;&nbsp;b'" is what worked for me.
25

I believe you're getting this because of the nature of html stripping spaces.

You could perhaps use the white-space: pre css property on whatever element you are rendering that text.

function MyCtrl($scope) {
  $scope.text = 'a      b';
}
...
<p style="white-space: pre">{{text}}</p>

I don't know alot about your application, but perhaps this will suffice.

Demo

7 Comments

But he said he tried &nbsp;s, which aren't stripped by HTML.
That's what I thought as well, until I tried it locally. The above is the only I got to work. It might suffice for OP, so worth trying I suppose.
Thank you, @Chris! Your style="white-space: pre" way is also working in Angular 2.
Worked for me :)
This worked for me as expected. Thank you for suggetion.
|
13

use '&#160;' instead of '&nbsp;' in template.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.