我想在ng-repeat完成角度后截断文本。
我发现一种简单的方法是使用limitTo过滤器来限制文本长度。 但是,我想通过它的实际宽度来翻译文本。
参考下面的页面,这是我的代码: Calling a function when ng-repeat has finished
HTML:
<div id="feedList">
<div ng-repeat="feed in feeds | orderBy: '-date' " feed-list-repeat-directive>
<h4><span>{{ feed.title }}</span></h4>
<!-- some more contents here -->
</div>
</div>
Javascript:
var app = angular.module('anApp', []);
app.controller('anCtrl', function($scope, $http) {
$http.get("data.php")
.success(function(response) {
$scope.feeds = response.feeds;
});
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
// truncate text by width #####################################
for(a=0; a<$('#feedList > div').length; a++){
$thisH4 = $('#feedList > div').eq(a).children('h4');
if($thisH4.width()<$thisH4.children('span').width()){
var txt = $thisH4.children('span').text();
while($thisH4.width()<$thisH4.children('span').width()){
txt = txt.substr(0,txt.length-1);
$thisH4.children('span').text(txt+'...');
}
};
};
//#######################################################
});
})
.directive('feedListRepeatDirective', function($timeout){
return {
restrict: 'A',
link: function(scope, element, attrs) {
if (scope.$last){
$timeout(function () {
scope.$emit('ngRepeatFinished');
});
};
}
}
});
问题是,当$ scope。$ on(&#39; ngRepeatFinished&#39;)运行时,数据实际上并未加载到dom,$ thisH4.children(&#39; span&#39;)。 width()返回0,
只有当我通过setTimeout()设置0.4秒的延迟时,它才有效,但我认为它没有意义。
有没有办法解决问题,或者我最好坚持使用limitTo过滤器?
感谢