AngularJS – How to create read more/less toggle using Directive?

AngularJS – How to create read more/less toggle using Directive?

In this post we will give you information about AngularJS – How to create read more/less toggle using Directive?. Hear we will give you detail about AngularJS – How to create read more/less toggle using Directive?And how to use it also give you demo for it if it is necessary.

Sometimes, We may require to make to show read more and read less functionality in our AngularJS application. We can make read more using AngularJS Directive.

So, In this example i going to give you full example of read more text example. I used “dd-text-collapse” Directive for after some text it will return “…” OR “(more)” etc that as you want. You can simply customize this Directive. You can also make toggle read more and read less using this Directive.

First, you can see bellow code of just Directive, i give you bellow code of Directive, you can use direct in your AngularJS application. so let’s see:

Directive:

app.directive('ddTextCollapse', ['$compile', function($compile) {


return {

restrict: 'A',

scope: true,

link: function(scope, element, attrs) {


/* start collapsed */

scope.collapsed = false;


/* create the function to toggle the collapse */

scope.toggle = function() {

scope.collapsed = !scope.collapsed;

};


/* wait for changes on the text */

attrs.$observe('ddTextCollapseText', function(text) {


/* get the length from the attributes */

var maxLength = scope.$eval(attrs.ddTextCollapseMaxLength);


if (text.length > maxLength) {

/* split the text in two parts, the first always showing */

var firstPart = String(text).substring(0, maxLength);

var secondPart = String(text).substring(maxLength, text.length);


/* create some new html elements to hold the separate info */

var firstSpan = $compile('<span>' + firstPart + '</span>')(scope);

var secondSpan = $compile('<span ng-if="collapsed">' + secondPart + '</span>')(scope);

var moreIndicatorSpan = $compile('<span ng-if="!collapsed">... </span>')(scope);

var lineBreak = $compile('<br ng-if="collapsed">')(scope);

var toggleButton = $compile('<span ng-click="toggle()">{{collapsed ? "(less)" : "(more)"}}</span>')(scope);


/* remove the current contents of the element

and add the new ones we created */

element.empty();

element.append(firstSpan);

element.append(secondSpan);

element.append(moreIndicatorSpan);

element.append(lineBreak);

element.append(toggleButton);

}

else {

element.empty();

element.append(text);

}

});

}

};

}]);

Ok, now i will make full example of read more and read less toggle using above directive, so let’s see and also you can see demo.

index.html

Also see:Angularjs nl2br(textarea newline and linebreak conversion) example with demo

<!DOCTYPE html>

<html>

<head>

<title>angularjs read more example</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>

<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<style type="text/css">

.collapse-text-toggle{

font-weight: bold;

}

</style>

</head>

<body>


<div ng-app="mainApp" ng-controller="myController" id="mainController" >

<p dd-text-collapse dd-text-collapse-max-length="230" dd-text-collapse-text="{{ longText }}"></p>

</div>


<script type="text/javascript">


var app = angular.module("mainApp", []);


app.controller('myController', function($scope, $timeout) {

$scope.longText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";


});


app.directive('ddTextCollapse', ['$compile', function($compile) {


return {

restrict: 'A',

scope: true,

link: function(scope, element, attrs) {


/* start collapsed */

scope.collapsed = false;


/* create the function to toggle the collapse */

scope.toggle = function() {

scope.collapsed = !scope.collapsed;

};


/* wait for changes on the text */

attrs.$observe('ddTextCollapseText', function(text) {


/* get the length from the attributes */

var maxLength = scope.$eval(attrs.ddTextCollapseMaxLength);


if (text.length > maxLength) {

/* split the text in two parts, the first always showing */

var firstPart = String(text).substring(0, maxLength);

var secondPart = String(text).substring(maxLength, text.length);


/* create some new html elements to hold the separate info */

var firstSpan = $compile('<span>' + firstPart + '</span>')(scope);

var secondSpan = $compile('<span ng-if="collapsed">' + secondPart + '</span>')(scope);

var moreIndicatorSpan = $compile('<span ng-if="!collapsed">... </span>')(scope);

var lineBreak = $compile('<br ng-if="collapsed">')(scope);

var toggleButton = $compile('<span ng-click="toggle()">{{collapsed ? "(less)" : "(more)"}}</span>')(scope);


/* remove the current contents of the element

and add the new ones we created */

element.empty();

element.append(firstSpan);

element.append(secondSpan);

element.append(moreIndicatorSpan);

element.append(lineBreak);

element.append(toggleButton);

}

else {

element.empty();

element.append(text);

}

});

}

};

}]);


</script>


</body>

</html>

I hope it can help you…

Hope this code and post will helped you for implement AngularJS – How to create read more/less toggle using Directive?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

60  +    =  62

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US