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.
- 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; } }
- Now, add that to your data binding as follows:
<span ng-bind="text.title | shortText" class="styles"></span>
Comments 0