【发布时间】:2015-08-08 13:06:58
【问题描述】:
我想截断表格单元格,但要尽可能多地将内容放入其中。我想在指令中实现一个出色的solution (fiddle)。我需要从
转换表格单元格<td>veryverylongunbreakablecontent</td>
到
<td>
<div class="container">
<div class="content">veryverylongunbreakablecontent</div>
<div class="spacer">veryv­erylon­gunbr­eakab­lecon­tent</div>
<span> </span>
</div>
</td>
这是一个简化的示例。我的表格单元由角度范围变量组成:
<td>{{ item.attribute.verylong }}</td>
到目前为止我想出的是一个指令
.directive('mwTableTruncate', function ($compile) {
return {
restrict: 'A',
templateUrl: 'modules/ui/templates/mwComponents/mwTableTruncate.html',
transclude: true,
compile: function( tElem, tAttrs ){
}
};
});
有模板
<div class="containerTest">
<div class="content">
<div ng-transclude></div>
</div>
<div class="spacer">
<div ng-transclude></div>
</div>
<span> </span>
</div>
现在我需要在分隔符 div 中的文本中每隔 5 个字符添加软连字符 (&shy;)。我该怎么做?
问题是我需要在翻译所有范围变量以添加软连字符后访问分隔符 div 文本。
edit#1 @sirrocco
我检查了编译、预链接、链接、链接后阶段的输出。这些阶段都不会转换范围变量。
链接阶段
.directive('mwTableTruncate', function ($compile) {
return {
restrict: 'A',
link: function (scope, iElem, attrs) {
console.log('link => ' + iElem.html());
console.log('link text => ' + iElem.text());
}
};
});
在控制台中给我:
link =>
{{ item.attributes.surname }}
link text =>
{{ item.attributes.surname }}
编译、预链接、后链接
.directive('mwTableTruncate', function ($compile) {
return {
restrict: 'A',
templateUrl: 'modules/ui/templates/mwComponents/mwTableTruncate.html',
transclude: true,
compile: function( tElem, tAttrs ){
console.log('Compile => ' + tElem.html());
return {
pre: function(scope, iElem, iAttrs){
console.log('pre link => ' + iElem.html());
console.log('pre link text => ' + iElem.text());
},
post: function(scope, iElem, iAttrs){
console.log('post link => ' + iElem.html());
console.log('post link text => ' + iElem.text());
//debugger;
}
};
}
};
});
控制台输出:
pre link => <div class="containerTest">
<div class="content">
<div ng-transclude=""></div>
</div>
<div class="spacer">
<div ng-transclude=""></div>
</div>
<span> </span>
</div>
pre link text =>
post link => <div class="containerTest">
<div class="content">
<div ng-transclude=""><span class="ng-binding ng-scope">
{{ item.attributes.surname }}
</span></div>
</div>
<div class="spacer">
<div ng-transclude=""><span class="ng-binding ng-scope">
{{ item.attributes.surname }}
</span></div>
</div>
<span> </span>
</div>
post link text =>
{{ item.attributes.surname }}
{{ item.attributes.surname }}
如您所见,{{ item.attributes.surname }} 变量都没有被翻译。
编辑#2
根据后期链接阶段超时功能的提示,我想出了这个解决方案:
指令
.directive('mwTableTruncate', function($timeout) {
return {
restrict: 'A',
templateUrl: 'modules/ui/templates/mwComponents/mwTableTruncate.html',
transclude: true,
compile: function() {
var softHyphenate = function (input) {
var newInput = '';
for (var i = 0, len = input.length; i < len; i++) {
newInput += input[i];
if (i % 5 === 0) {
newInput += '­';
}
}
return newInput;
};
return {
post: function (scope, iElem) {
$timeout(function () {
var text = iElem.find('div.spacer').text().trim();
// add tooltips
iElem.find('div.content').prop('title', text);
// add soft hyphens
var textHyphenated = softHyphenate(text);
iElem.find('div.spacer').html(textHyphenated);
});
}
};
}
};
});
模板
<div class="containerTest">
<div ng-transclude class="content"></div>
<div ng-transclude class="spacer"></div>
<span> </span>
</div>
使用孤立的示波器 sirrocco rbaghbanli 会是什么样子?
【问题讨论】:
-
当变量被解释时,使用链接函数而不是编译应该会让你进入链接后阶段。您是否尝试过使用链接而不是编译?
-
是的,更新了问题
-
在 $timeout 函数中移动 postlink 代码,这应该确保摘要周期已经运行
-
我认为您也可以使用隔离范围,传入 longText 变量,然后您无需等待超时即可访问。然后,您还可以删除 transclude 属性。 rbaghbanli 从一开始就是对的。
-
你会如何用一个孤立的范围来做呢?
标签: angularjs angularjs-directive