【发布时间】:2019-02-28 00:40:17
【问题描述】:
我有以下带有“dotdotdot”css 类的模板,它添加 省略号溢出正确。
<div class="dotdotdot">{{data.trip.name}}</div>
我在这里要做的是实现一个指令,该指令在 省略号仅被激活。
这是指令中的当前代码:
import { Directive, OnInit, ElementRef } from '@angular/core';
declare var $: any;
@Directive({
selector: '.dotdotdot'
})
export class DotdotdotDirective implements OnInit {
private el: HTMLElement;
constructor(elRef: ElementRef) {
this.el = elRef.nativeElement;
}
ngOnInit() {
if (this.isEllipsisActive(this.el)) {
// TODO add title attribute to the div with value from text
$(this.el).tooltip();
}
}
isEllipsisActive(e) {
return (e.offsetWidth < e.scrollWidth);
}
}
我上面的代码有两个问题:
- isEllipsisActive 不起作用,我需要识别省略号的方法。
- 我需要知道如何动态添加title或[title]属性 来自 $(this.el)。该值是 div 中的文本。
谢谢!
【问题讨论】:
-
它可以在没有 jQuery 的情况下工作。见this stackblitz。
-
可以在here 找到更可靠的答案。它适用于
ngAfterViewChecked,因此不需要setTimeout+ 如果在innerText更改时不再需要,也会清除标题。
标签: jquery angular angular-directive