{"id":952,"date":"2024-04-14T14:31:57","date_gmt":"2024-04-14T14:31:57","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=952"},"modified":"2025-05-15T04:46:24","modified_gmt":"2025-05-15T04:46:24","slug":"angularjs-slugify-filter","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/angularjs-slugify-filter\/","title":{"rendered":"AngularJS \u2013 Slugify Filter"},"content":{"rendered":"\n<p>A custom AngularJS filter for converting a string into a url slug. A slug is a url &amp; seo friendly string containing only lowercase alphanumeric characters and hyphens \u201c-\u201c. Slugs are often used for including titles in URLs for SEO. URL slug in angular js <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">app.filter('slugify', function() {\n  return function(input) {\n    if (!input) return '';\n\n    \/\/ Convert the string to lowercase\n    var slug = input.toLowerCase();\n\n    \/\/ Remove special characters\n    slug = slug.replace(\/[^a-z0-9\\s-]\/g, '');\n\n    \/\/ Replace spaces and underscores with hyphens\n    slug = slug.replace(\/[\\s_]+\/g, '-');\n\n    \/\/ Remove any leading or trailing hyphens\n    slug = slug.replace(\/^-+|-+$\/g, '');\n\n    return slug;\n  };\n});\n<\/pre><\/div>\n\n\n\n<p>Use the Slug Filter in Your Controller or View<\/p>\n\n\n\n<p>Once you have the filter in place, you can use it in your AngularJS controller or directly in the view.<\/p>\n\n\n\n<p><strong>In Controller:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">app.controller('MyController', function($scope, $filter) {\n  $scope.title = &quot;My Awesome Blog Post!&quot;;\n  $scope.slug = $filter('slugify')($scope.title);\n});\n<\/pre><\/div>\n\n\n\n<p>In View:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;div ng-controller=&quot;MyController&quot;&gt;\n  &lt;p&gt;Original Title: {{ title }}&lt;\/p&gt;\n  &lt;p&gt;URL Slug: {{ title | slugify }}&lt;\/p&gt;\n&lt;\/div&gt;\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>If your original title is <code>\"Angular js slug creation!\"<\/code>, the slug generated would be <code>angular-js-slug-creation.<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Lowercase Conversion<\/strong>: The string is converted to lowercase for uniformity.<\/li>\n\n\n\n<li><strong>Special Characters Removal<\/strong>: All special characters except spaces and hyphens are removed.<\/li>\n\n\n\n<li><strong>Space Replacement<\/strong>: Spaces are replaced with hyphens to create the slug.<\/li>\n\n\n\n<li><strong>Hyphen Trimming<\/strong>: Any leading or trailing hyphens are removed.<\/li>\n<\/ul>\n\n\n\n<p>This method allows you to easily create <strong>SEO-friendly URL slugs<\/strong> within your <strong><a href=\"https:\/\/www.appfinz.com\/blogs\/category\/angularjs\/\">AngularJS application<\/a><\/strong>.<\/p>\n\n\n\n<p>If you\u2019re planning to revamp your online presence, don\u2019t miss exploring our <a href=\"https:\/\/www.appfinz.com\/website-designing-company-in-delhi\">website designing services<\/a> and custom <strong><a href=\"https:\/\/www.appfinz.com\/website-development-company-in-delhi\">website development<\/a><\/strong> solutions tailored to your business needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A custom AngularJS filter for converting a string into a url slug. A slug is<\/p>\n","protected":false},"author":1,"featured_media":1268,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angularjs"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=952"}],"version-history":[{"count":3,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/952\/revisions"}],"predecessor-version":[{"id":1338,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/952\/revisions\/1338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1268"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}