Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 输入内容的slice()_Javascript_Angular_Slice - Fatal编程技术网

Javascript 输入内容的slice()

Javascript 输入内容的slice(),javascript,angular,slice,Javascript,Angular,Slice,我正在处理Angular中的一个赋值,用户必须在输入字段中放入一些内容,当超过27个时,结果应该是前27个字符,后跟“…”。我使用的是slice()方法,但即使代码编译并运行,也不会缩短结果。我仍然得到整个输入,即使它是,比方说,50个字符 HTML: 怎么了?也许用正则表达式代替会更好?谢谢你的提示和建议!:) 这不是正确的方法,因为您可能不想因为修剪数据而丢失数据。最好的做法是存储完整的字符串,并在末尾使用…仅显示其中的一部分 为此,请使用文本溢出:省略号。如果文本溢出固定宽度,则会在末尾追

我正在处理Angular中的一个赋值,用户必须在输入字段中放入一些内容,当超过27个时,结果应该是前27个字符,后跟“…”。我使用的是slice()方法,但即使代码编译并运行,也不会缩短结果。我仍然得到整个输入,即使它是,比方说,50个字符

HTML:


怎么了?也许用正则表达式代替会更好?谢谢你的提示和建议!:)

这不是正确的方法,因为您可能不想因为修剪数据而丢失数据。最好的做法是存储完整的字符串,并在末尾使用
仅显示其中的一部分

为此,请使用
文本溢出:省略号。如果文本溢出固定宽度,则会在末尾追加


这里有一个完整的指南:

在您创建分配给它的较短字符串后,长字符串只会更改它。。。 由此:

 this.shorter= this.input1 
为此:

 this.input1 = this.shorter

使用子字符串代替切片

  shortener() {
    let inputLength: number = this.input1.length
    console.log(inputLength)
    if (inputLength >27) {
    this.shorter = this.input1.substring(0,27)+"..."
    } else {
    this.notification = "Jest mniej niż 27 znaków. Szkoda skracać!"
    }
    console.log(this.shorter)
  }

并删除这一行
this.shorter=this.input1

您能提供一个JSbin或JSFIDLE示例吗?@ShobiDobi-谢谢!但是,在将切片部分分配给新变量的情况下,我真的会丢失数据吗?即使没有丢失数据,将其分配给新变量也是一种开销。如果有现成的解决方案可用于此类用例,那么为什么要花费这么多精力呢
 this.input1 = this.shorter
  shortener() {
    let inputLength: number = this.input1.length
    console.log(inputLength)
    if (inputLength >27) {
    this.shorter = this.input1.substring(0,27)+"..."
    } else {
    this.notification = "Jest mniej niż 27 znaków. Szkoda skracać!"
    }
    console.log(this.shorter)
  }