我想用ngFor做一个少显示多效果的显示,这样通过点击查看更多按钮,所有文本都会展开,而不仅仅是一个,我希望如果文本超过例如150个字符,我会应用部分文本+"…"把剩下的文字藏起来.当我单击"查看更多"按钮时,所有文本的其余部分都会出现,而不仅仅是一个文本,并且"查看更多"按钮文本会更改为"查看更少".

我没有太大的进步,所以我没有一个完整的代码.

<td *ngFor=" let test of testData?.testDataDescription?.testDataDescriptionCode"> 

     <div #myDivText>
          {{handleBigText(test?.descriptionArea?.summary, myDivText)}}
     </div>

     <span #extend 
     (click)="collapseText( test?.description?.textSumarryDescription, myDivText, extend)">
     <img src="/assets/images/arrow_down.svg"/>
          view more
     </span>
</td>





  public maxLength = 150;
  public resizeText = 3;
  public sizeDescription = this.maxLength;
  public sizeDescriptionDots = this.maxLength + this.resizeText;

  collapseText(text: string, myDivText: Element, extend: Element) {
    const size = text.length - 3;
    if (myDivText.innerHTML.length <= size) {
      myDivText.innerHTML = text;
      extend.innerHTML = `<img src="/assets/images/arrow_up.svg" />
              see less`;
    } else {
      myDivText.innerHTML = `${text.substring(0, this.sizeDescriptionDots)}...`;
      extend.innerHTML = `<img src="/assets/images/arrow_down.svg"/>
             view more`;
    }
  }

  handleBigText(text: string, myDivText: Element) {
    if (text) {
      const textSize = this.maxLength + this.resizeText;
      this.sizeDescription = textSize;
      this.sizeDescriptionDots = textSize;

      return text.length > textSize
        ? `${text.substring(0, textSize)}...`
        : text;
    }

    return '-';
  }

推荐答案

你把事情搞得太复杂了.无需实际修改原始文本本身.

  1. 让我们使用Angular's | slice管道来限制显示的文本.https://angular.io/api/common/SlicePipe

  2. 让我们使用*ngIf来有条件地显示更多或更少的文本.

<hello name="{{ name }}"></hello>
<p> {{ showMore ? text : (text | slice: 0:150) }} <span *ngIf="!showMore">...</span>
  <a href="javascript:;" *ngIf="!showMore" (click)="onShow()">[Show More]</a>
  <a href="javascript:;" *ngIf="showMore" (click)="onShow()">[Show Less]</a>
</p>
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  showMore = false;

  onShow () {
    this.showMore = !this.showMore;
  }

  text = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;
}

工作示例:https://stackblitz.com/edit/angular-easy-show-more-6ispwx?file=src%2Fapp%2Fapp.component.html

Javascript相关问答推荐

在JS中转换mysql UTC时间字段?

获取最接近的父项(即自定义元素)

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

通过嵌套模型对象进行Mongoose搜索

扫描qr code后出错whatter—web.js

D3 Scale在v6中工作,但在v7中不工作

当作为表达式调用时,如何解析方法decorator 的签名?

在286之后恢复轮询

使用LocaleCompare()进行排序时,首先使用大写字母

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

如何在箭头函数中引入or子句?

回溯替代方式

AJAX POST在控制器中返回空(ASP.NET MVC)

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

Next.js中的服务器端组件列表筛选

警告框不显示包含HTML输入字段的总和

重新呈现-react -筛选数据过多

JavaScript将字符串数字转换为整数

用Reaction-RT-Chart创建实时条形图

Promise.race()返回已解析的promise ,而不是第一个被拒绝的promise