AngularJS – Slugify Filter

A custom AngularJS filter for converting a string into a url slug. A slug is a url & seo friendly string containing only lowercase alphanumeric characters and hyphens “-“. Slugs are often used for including titles in URLs for SEO.
(function () {
'use strict';
angular
.module('app')
.filter('slugify', Filter);
function Filter() {
return function (input) {
if (!input)
return;
// make lower case and trim
var slug = input.toLowerCase().trim();
// replace invalid chars with spaces
slug = slug.replace(/[^a-z0-9\s-]/g, ' ');
// replace multiple spaces or hyphens with a single hyphen
slug = slug.replace(/[\s-]+/g, '-');
return slug;
};
}
})();
Example usage of AngularJS Slugify Filter
<a href="{{post._id}}/{{post.title | slugify}}">{{post.title}}</a>
Check This Also
Difference between AngularJS and Angular 2 Angular 9: What to Expect in New Version of Angular Alert (Toaster) Notifications in Angular 8 MEVN Stack For Beginners Why Node Js is First Choice For Deploying Large Applications Back up and Restore Mongo DB DatabaseTags: website development company in delhi|| website designing company in delhi || Website Developer in Delhi || magento development company in delhi || web developer in delhi