Angular 2 Read More Directive

前端 未结 10 1833
一个人的身影
一个人的身影 2020-12-01 09:41

I need to build a readmore directive in Angular2. What this directive will do is for collapse and expand long blocks of text with \"Read more\" and \"Close\" links. Not on t

10条回答
  •  悲&欢浪女
    2020-12-01 10:11

    If you want to display the text to the maximum number of chars without cutting any word, change this line of code:

    this.currentText = this.text.substring(0, this.maxLength) + "...";
    

    To:

    this.currentText = this.text.substring(0, this.maxLength);
    this.currentText = this.currentText.substr(0, Math.min(this.currentText.length, this.currentText.lastIndexOf(" ")))
    this.currentText = this.currentText + "..."
    

提交回复
热议问题