Ng-Bind-Html-7

 

In this example, we are creating a custom filter that will basically sub-string till the provided length and then create a anchor tag from it Here is the custom filter: app.filter('ellipsis', function () { return function (text, length) { if (text.length > length) { return text.substr(0, length) + "" + text.substr(length, 10) + ""; }; return text; }; }); And html side will look like:


 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-sanitize.js"></script> </head> <body ng-app="myApp"> <div ng-controller="myController"> <div ng-bind-html="text | ellipsis:3"></div> <div ng-bind-html="text | ellipsis:4"></div> <div ng-bind-html="text | ellipsis:5"></div> </div> <script> var app = angular.module("myApp", ['ngSanitize']); app.controller('myController', ['$scope', function ($scope) { $scope.text = "LearnIt"; }]); app.filter('ellipsis', function () { return function (text, length) { if (text.length > length) { return text.substr(0, length) + "<a href='http://learnit.visrosoftware.com/'>" + text.substr(length, 10) + "</a>"; }; return text; }; }); </script> </body> </html>
Output