【发布时间】:2016-08-29 16:10:13
【问题描述】:
这发生在 Angular 指令链接函数内部。
所以我有这个工作,但我试图使用角度的指令 $element 做得更好。似乎我陷入了无限循环。我做错了什么 - 我认为这很容易,但我显然在这里遗漏了一些基本的东西。
这行得通:
$timeout(function(){
$('.add-ellipsis').each(function() {
var $ellipsisText = $(this);
while ($ellipsisText.outerHeight() > 30) {
$ellipsisText.text(function(index, text) {
return text.replace(/\W*\s(\S)*$/, '...');
});
}
});
}, false);
示例:https://jsfiddle.net/rooksstrife/7chxph91/12/
尝试将其更改为: 注意($element.context.firstChild.firstElementChild.innerText = "一长串文本".
$timeout(function(){
while ($element.outerHeight() > 30) {
$element.context.firstChild.firstElementChild.innerText.replace(/\W*\s(\S)*$/, '...');
}
}, false);
【问题讨论】:
-
为什么不使用 $watch 来注册 outerHeight 的变化?
-
@Luxor001不明白你的意思——我不需要观察不断变化的高度——只需要调整一次。如果这就是你所指的。
标签: javascript jquery angularjs angularjs-directive