私はAngular2でreadmore指令を作成する必要があります。このディレクティブが行うことは、 "続きを読む"と "閉じる"リンクを使用してテキストの長いブロックを折りたたんで展開することです。文字カウントに基づくものではなく、指定された最大高さに基づいています。角2続きを読む
<div read-more [maxHeight]="250px" [innerHTML]="item.details">
</div>
この特定のケースの要素の高さを取得/設定する最も信頼できる方法は、誰でもガイドしてください。
この具体的な指令をどのように実装できるかについてのガイドラインも高く評価されます。私は私の要件を満たしている以下のコンポーネントを構築することができる午前Andzhikから助けを借りて
:
私はこのhttps://github.com/jedfoster/Readmore.js
ソリューションのようなものを構築する必要があります。
import { Component, Input, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'read-more',
template: `
<div [innerHTML]="text" [class.collapsed]="isCollapsed" [style.height]="isCollapsed ? maxHeight+'px' : 'auto'">
</div>
<a *ngIf="isCollapsable" (click)="isCollapsed =! isCollapsed">Read {{isCollapsed? 'more':'less'}}</a>
`,
styles: [`
div.collapsed {
overflow: hidden;
}
`]
})
export class ReadMoreComponent implements AfterViewInit {
//the text that need to be put in the container
@Input() text: string;
//maximum height of the container
@Input() maxHeight: number = 100;
//set these to false to get the height of the expended container
public isCollapsed: boolean = false;
public isCollapsable: boolean = false;
constructor(private elementRef: ElementRef) {
}
ngAfterViewInit() {
let currentHeight = this.elementRef.nativeElement.getElementsByTagName('div')[0].offsetHeight;
//collapsable only if the contents make container exceed the max height
if (currentHeight > this.maxHeight) {
this.isCollapsed = true;
this.isCollapsable = true;
}
}
}
使用法:
<read-more [text]="details" [maxHeight]="250"></read-more>
任意の改善があるかもしれません場合は、提案すること自由に感じなさい。
のsetTimeout内部currentHeight(_ => {...})は、いくつかのウィンドウを排除する検索角動作の変化の検出中にリサイズの問題が発生します。 http://stackoverflow.com/questions/38930183/angular2-expression-has-changed-after-ws-checked-binding-to-div-width-wi – chenk