Remove the rest of text after a specific character in AngularJS and TypeScript.


Remove the rest of text after a specific character in AngularJS and TypeScript.

When there is a need for removing reminder of text after a certain character, the best way would be to create a custom filter in AngularJS. Using substring() function, we’re going to find the specified character, e.g. “/n”, and then remove everything after that.

  1. Create a filter, let’s call it shortText.ts:


export function shortText() {

return function (input:any) {

var FullSentence = input.indexOf(‘\n’);

let subString= input;

subString = subString.substring(0, FullSentence != -1 ? FullSentence : subString.length);

return subString; } }

  1. Now, add that to your data binding as follows:


<span  ng-bind="text.title | shortText" class="styles"></span>

Ramin Ahmadi
I am a full-stack front end developer with over 5 years experience in web design and development. I have worked with a wide variety of environments and languages including Angular, TypeScript, NodeJS, Restful API, Microservices, Atomic design, JQuery, Material design, Progressive Web Apps, DevOps, and many Azure tools. I make it a goal to automate myself out of routine tasks in my daily work. My motto is, ‘write human readable code, lean and clean’.

Comments 0

Your email address will not be published. Required fields are marked *

Remove the rest of text after a specific character in AngularJS and TypeScript.

log in

Captcha!

reset password

Back to
log in
Choose A Format
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
List
The Classic Internet Listicles
Image
Photo or GIF
Gif
GIF format