Angular 2 Read More Directive

前端 未结 10 1838
一个人的身影
一个人的身影 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:06

    Just a slight improvement of @Andrei Zhytkevich code (useful for markdown)

    import {
      Component,
      AfterViewInit,
      ViewChild,
      ElementRef,
      Attribute,
      ChangeDetectionStrategy } from '@angular/core';
    
    @Component({
      changeDetection: ChangeDetectionStrategy.OnPush,
      selector: 'ui-read-more',
      template: `
        
    `, styles: [` :host{ display: block; } .collapsed { overflow: hidden; padding-bottom: 1rem; } .read-more{ display: flex; justify-content: flex-end; } `] }) export class UiReadMoreComponent implements AfterViewInit{ @ViewChild('wrapper') wrapper: ElementRef; isCollapsed: boolean = true; private contentHeight: string; private _height: string; constructor(@Attribute('height') public height: string = '') { this._height = height; } ngAfterViewInit() { this.contentHeight = this.wrapper.nativeElement.clientHeight + 'px'; } onIsCollapsed(){ this.isCollapsed = !this.isCollapsed; this._height = this.isCollapsed ? this.height : this.contentHeight; } }

    Usage

    
     
        {{post.content}}
     
    
    

提交回复
热议问题