0

In angular controller file, I want to add suffix ... to make shorten a string if it is lengthy but now ... is now a spread operator in es6 so try using … and tried but it prints the text … not ... in HTML

trial 1

let shortName = `${longString.substr(0,9)}${(longString.length>9? '…':'';`

trial 2

$sce.trustAsHtml(shortName + '…')

but in HTML it show &hellp; not the ...

Note: I can use ng-bind-html but this string is used as a label in chart which is in <canvas> and that is created by the angular-chrat.js library.

I do not want to manipulate it through library but using core Javascipt.

4
  • Why not using its utf8 equivalent? Commented Aug 31, 2017 at 17:34
  • and what it will be? Please suggest how to do Commented Aug 31, 2017 at 17:36
  • I mentioned that in my answer. Also a triple dot should work fine too, doesn't it? Commented Aug 31, 2017 at 17:43
  • Yes but my question is how to use html entities . this is specific to &hellip; suppose I want to use &copy; than? Commented Aug 31, 2017 at 17:45

3 Answers 3

2

You can use the utf-8 equivalent character if you have set your charset to utf-8: '…'

You mentioned you are using it in canvas? If so you have to convert your entity into a symbol instead. You can use a DOMParser for that.
Here is a way to do that

Sign up to request clarification or add additional context in comments.

8 Comments

yes I can use but I want to know how to use html entities in javascript.
Thank you for the reference, this is working fine but still checking does angular have the solution for this?
What do you mean by checking?
I means looking for angularJS solution also.yes this is working and I have accepted the answer.
I think the angular version would be to use ng-bind-html. Even with $sce, you kinda need to use ng-bind-html
|
0

Have you tried ng-bind-html? Click here to check documentation

1 Comment

I can not do that in HTML, this text is being used as a label in graph which is in <canvas>
0

You can parse HTML entities before cancating into string

let e = document.createElement('div');
e.innerHTML = '&hellip;';

let shortName = `${longString.substr(0,9)}${(longString.length>9? ${e.innerText}:'';`

or can use DOMParser

let p = new DOMParser();
let d = p.parseFromString('&hellip;', 'text/html');
let shortName = `${longString.substr(0,9)}${(longString.length>9? ${d.documentElement.textContent}:'';`

4 Comments

I dont want to put this string in any other HTML element such as div you suggested
and innerText or innerHTML in last line? I think there is typo
That is not typo. we just need the symbol, so it is obtained by innerText
@pro.mean entity can't be used without parsing it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.