1

In my html, I used pipe {{dat.url.split(":")[1]|slice:2}}(Template file).This is how I showed the data from the database.

The DB data is like http://192.168.103.42:8888.

But I should show the data like 192.168.103.42 from this.

The problem is, when the user selects the edit option, by default it should show their saved data. For that I have to again use this dat.url.split(":")[1]|slice:2 in my .ts file(form field).

But it shows error in slice:2

How to use slice inside .ts file in angular?

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
  • Please use like this [click here](https://stackoverflow.com/questions/35144821/angular-use-pipes-in-services-and-components) – Sourav Golui Nov 08 '19 at 07:42

4 Answers4

2

Try like this:

 let temp = this.dat.url.substring(this.dat.url.lastIndexOf("/") + 1);

 this.dat.url = temp.substr(0, temp.lastIndexOf(":"));

Demo

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
1

Also you may try this:


 this.dat.url = this.dat.url.split(":")[1].slice(2)

Demo

Roma Ruzich
  • 692
  • 5
  • 18
1

Typescript is a super set of Javascript. So slicing is same as you do in JavaScript.

1st method is using slice()

var str = 'unbelievable';
// pass (zero-based) index location 2 to slice
var str2 = str.slice(2);
console.log(str2); // believable

2nd method is using substring()

var str = 'unbelievable';
var str2 = str.substring(2);
console.log( str2 ); // believable

3rd method is using substr()

var str = 'unbelievable';
var str2 = str.substr(2);
console.log(str2); // believable
Rohan Shenoy
  • 805
  • 10
  • 23
1

You can use regex for this

let url = "http://192.168.103.42:8888"
let trimed = url.replace(/:[^:]+$/, "");
console.log(trimed);
Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20