각도 2로 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>
, 제안 주시기 바랍니다.
찾기 currentHeight (_ => {.....}) 일부 창을 제거 할 것 각도 실행 변경 감지 중에 크기 조정 문제가 발생합니다. http://stackoverflow.com/questions/38930183/angular2-expression-has-changed-after-ws-checked-binding-to-div-width-wi를 참조하십시오. – chenk