I'm new to angularjs, i need to display multiline text into 2 lines(max display 225 chars) and add a hyperlink to view more text, and less link to hide more text. I tried it in using jquery need some help in integrating into angular.
Angular Code
<div class="row" ng-repeat="note in noteList">
<div>{{note.title}}</div>
<div>
<span ng-if="note.length>225">{{note.body | limitTo:225:1}}
<a class="more-link expand-link" data-rel="#prj-note-{{$index + 1}}">More...</a></span>
<span id="prj-note-{{$index + 1}}" class="prj-note" ng-if="note.length>225">{{note | limitTo:5000:225}}
<a class="collapse-link" data-rel="#prj-note-{{$index + 1}}">Less...</a></span>
<span ng-if="note.length<225">{{note.body}}</span>
</div>
</div>
Jquery
$('.expand-link').on('click', function(e) {
e.preventDefault();
$($(this).attr('data-rel')).show('fade', 300);
$(this).css('display', 'none');
});
$('.collapse-link').on('click', function(e) {
e.preventDefault();
var tempId = $(this).attr('data-rel');
$(tempId).hide();
$($("a[data-rel='" + tempId + "']")[0]).css('display', 'block');
})
Css
.prj-note{display:none;}
Thanks in advance for help!
Every time We have such case when it is possible to reuse such approach We should create directive ( or component ). Below I created ready to use directive more-less-text which can be used in
ng-repeat
or standalone or as You need.More about solution - We can use here
ng-show
andng-if
without additional jquery events and this is exactly what I have done in more-less-text directive.Using in ng-repeat should look like:
Some additional info
I used directive instead of component because stackoverflow not gives currently possibility to create code snippet with something more than AngularJs 1.2.
Code is using ES6 features like multi-line strings, but can be easily refactored to ES5.