【发布时间】:2020-05-27 10:45:18
【问题描述】:
我有一个角度应用程序,我想限制段落中的字符(p)。 这样它就会在说 50 个字符后返回。
所以我有这个模板:
<section class="echeq-element-html-display border-box"
[ngClass]="{isYoutubeVideo: isYoutubeVideo}"
[innerHTML]="doHtmlDisplay(echeqElement)"
></section>
ts:
@Component({
selector: 'app-html-display',
templateUrl: './html-display.component.html',
styleUrls: ['./html-display.component.css']
})
doHtmlDisplay(eCheqElement: EcheqElement): SafeHtml {
return this.returnSafeHtml(this.getHtml(eCheqElement));
}
这是我的 CSS:
p {
text-decoration: underline;
text-overflow: ellipsis; /* will make [...] at the end */
width: 50px; /* change to your preferences */
white-space: nowrap; /* paragraph to one line */
overflow: hidden; /* older browsers */
}
但这不起作用。
那么我必须改变什么?
谢谢
还有一个main.css:
p {
margin: 0 0 0.75em;
}
当然,我只是想获得该组件的最大字符数,而不是在整个应用程序中。
在 ts 脚本中,我现在是这样的:
truncateText(selector, maxLength) {
let element = document.querySelector(selector),
truncated = element.innerText;
if (truncated.length > maxLength) {
truncated = truncated.substr(0, maxLength) + '...';
}
return truncated;
}
在模板中:
<section class="echeq-element-html-display border-box" (input)="truncateText('p', 10)" [ngClass]="{isYoutubeVideo: isYoutubeVideo}" [innerHTML]="doHtmlDisplay(echeqElement)"></section>
但没有任何改变。
【问题讨论】:
-
能给个echeqElement示例内容吗?
-
你不会用纯javascript吧?
标签: javascript html css angular