Ken Ryan Ken Ryan - 4 months ago 15
AngularJS Question

How to make Angular Routing with title instead of id?

I am building a blog with angular and am currently working on the routes. I have the routes working with routeParams but angular gives it a random number and the url ends like


I want it to either automatically grab the title and use it as the parameter on the URL or use the property called id on the json file such as

Is this possible with


angular.module('app', [

.config(['$routeProvider', '$urlRouterProvider', function($routeProvider, $urlRouterProvider){
.when('/posts', {
templateUrl: 'views/posts.html',
controller: 'PostListController'
.when('/posts/:id', {
templateUrl: 'views/singlepost.html',
controller: 'PostDetailController'
redirectTo: '/'


angular.module('app.controllers', ['app.directives'])
/* Controls the Blog */
.controller('PostListController', ['$scope', '$http', function($scope, $http){
$scope.posts = response;

.controller('PostDetailController', ['$scope', '$http', '$routeParams', '$sce', function($scope, $http, $routeParams, $sce){
$ = response[$];


<div class="container" id="postList">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div ng-repeat="post in posts |" class="post">
<h2 class="blog-title"><a href="#/posts/{{posts.indexOf(post)}}">{{post.title}}</a></h2>
<span class="date">Posted on {{ | date:'MMM dd, yyyy'}}</span>
<div class="row top10">
<div class="col-md-8">
<div class="post-content">{{post.headline}}</div>
<a class="read-more" href="#/posts/{{posts.indexOf(post)}}">Read more</a>


<div class="container" id="singlePost">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<span class="date">Posted on {{ | date:'MMM dd, yyyy'}}</span>

<div class="postContent" ng-bind-html="post.content"></div>


You will need to change two main things, first how the URLs are generated, and second how the post is recovered.

Generate URL from title

Not any title will be valid as URL, so you need to use slugs.

In the view:

<a class="read-more" href="#/posts/{{getSlug(post.title)}}">Read more</a>

In the controller:

$scope.getSlug = function(text){
  return text.replace(/\W+/g, '-');

Search post from slug

Currently, you are just fetching the post from the array as an index, which is conceptually wrong (you will notice this after remove one post). Instead, make a search through the array. This will be really simple if you use lodash.

Something like this would work:

$ = _.find(response, function(post) {
  return post.title.replace(/\W+/g, '-') === $;