Reputation: 10039
I want to show a "Read More" link if my paragraph height reaches 200px. What is an elegant way of doing it using Angular?
My elements -
<section class="mynotes" ng-if="MyController.mynotes">
<p ng-bind="MyController.mynotes" ng-class="{'showMore': more, 'showLess': !more}"></p>
<section ng-click="MyOtherController.togglearrow($event)">
<i class="down-arrow"></i>
<span ng-click="more = !more">{{more ? 'Less' : 'More'}}</span>
</section>
I want to hide the arrow and the Less/More text based on the p height. Any ideas?
Upvotes: 2
Views: 2283
Reputation: 123749
Here is a simple and configurable directive i just came up with for show less feature.
.directive('moreless', function($timeout){
//Default options, default height of 200
var defOption = {
height:200
};
return {
restrict:'EA',
transclude:true, //transclusion enabled
scope:{}, //scoped to isolate since this adds functions on scope
template:'<div class="showless"><div ng-transclude class="content" ng-style="style"></div><section class="trigger" ng-if="showSection"> \
<i class="down-arrow"></i> \
<span ng-click="toggleMore()">{{{true:"More", false:"Less"}[more]}}</span> \
</section></div>',
link:function(scope, elm, attrs){
var origHeight,
//Construct the options from default options and override by any thing specified
options = angular.extend({}, angular.copy(defOption), scope.$eval(attrs.options));
//Initialize
initialize();
function initialize(){
scope.toggleMore = toggleMore;
scope.toggleSection = toggleSection;
//Do not show showless section initially
scope.toggleSection(false);
//Wait for one digest cycle to complete and intialize showless componenet
$timeout(_initShowLess);
}
function _initShowLess(){
//Set initial height and get the height
origHeight = elm.find('.content').height();
scope.style = {height:origHeight};
//compare the height to allowed height and togglesection and set showmore accordingly
if(origHeight > options.height){
scope.toggleSection(true);
scope.style.height = options.height;
scope.toggleMore();
}
}
function toggleMore(){
expandCollapse(scope.more);
scope.more = !scope.more;
}
function toggleSection(show){
scope.showSection = show;
}
function expandCollapse(expand){
var height = expand ? origHeight : options.height;
scope.style.height = height;
}
}
}
});
Default setting is set at 200 height, you can configure through options attribute. SO the usage would be similar to:
<moreless options="{height:300}">
<p ng-bind="mynotes2"></p>
</moreless>
Inline Demo'
var app = angular.module('plunker', []);
app.directive('moreless', function($timeout) {
var defOption = {
height: 200
};
var defOffset = 10;
return {
restrict: 'EA',
transclude: true,
scope: {},
template: '<div class="showless"><div ng-transclude class="content" ng-style="style"></div><section class="trigger" ng-if="showSection"> \
<i class="down-arrow"></i> \
<span ng-click="toggleMore()">{{{true:"More", false:"Less"}[more]}}</span> \
</section></div>',
link: function(scope, elm, attrs) {
var origHeight,
options = angular.extend({}, angular.copy(defOption), scope.$eval(attrs.options));
initialize();
function initialize() {
scope.toggleMore = toggleMore;
scope.toggleSection = toggleSection;
scope.toggleSection(false);
$timeout(_initShowLess);
}
function _initShowLess() {
origHeight = elm.find('.content').height();
scope.style = {
height: origHeight
};
if (origHeight > options.height) {
scope.toggleSection(true);
scope.style.height = options.height;
scope.toggleMore();
}
}
function toggleMore() {
expandCollapse(scope.more);
scope.more = !scope.more;
}
function toggleSection(show) {
scope.showSection = show;
}
function expandCollapse(expand) {
var height = expand ? origHeight : options.height;
scope.style.height = height;
}
}
}
}).controller('MainCtrl', function($scope) {
$scope.mynotes = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.(The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.';
$scope.mynotes2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
})
/* Put your css in here */
.showless {
position: relative;
}
.showless .content {
overflow: hidden;
transition: all linear 0.5s;
-webkit-transition: all linear 0.5s;
}
.showless .trigger {
background: #cdcdcd;
display: inline-block;
}
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://code.angularjs.org/1.3.10/angular.js" data-semver="1.3.10"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div>
<moreless>
<p ng-bind="mynotes"></p>
</moreless>
</div>
<div>
<moreless options="{height:100}">
<p ng-bind="mynotes"></p>
</moreless>
</div>
<div>
<moreless options="{height:300}">
<p ng-bind="mynotes2"></p>
</moreless>
</div>
</body>
</html>
Upvotes: 5
Reputation: 54821
I've coded the below directive from the top of my head, but you should be able to get the idea.
angular..directive('setMore',function(){
return {
restrict: 'A',
link: function($scope,$el,$attr) {
$scope.$watch(function() {
return $el.height();
},function(height) {
$scope.more = height > ~~$attr['setMore'];
}
});
}
};
});
You can use it like this.
<p set-more="100" ng-bind="MyController.mynotes" ng-class="{'showMore': more, 'showLess': !more}"></p>
Upvotes: 2