如何使用title而不是id进行Angular Routing?

Ken*_*yan 5 javascript angularjs angular-routing

我正在建立一个有角度的博客,我目前正在路线上工作.我有路由使用routeParams但angular给它一个随机数,url结束http://localhost/kensproblems/#/posts/2

我希望它能够自动获取标题并将其用作URL上的参数,或者使用json文件中名为id的属性,例如 http://localhost/kensproblems/#/posts/title-of-post

这有可能$routeParams吗?

app.js

angular.module('app', [
    'ngRoute',
    'app.controllers',
    'ui.router',
    'ngSanitize'
])

.config(['$routeProvider', '$urlRouterProvider', function($routeProvider, $urlRouterProvider){
    $routeProvider
    .when('/posts', {
        templateUrl: 'views/posts.html',
        controller: 'PostListController'
    })
    .when('/posts/:id', {
        templateUrl: 'views/singlepost.html',
        controller: 'PostDetailController'
    })
        .otherwise({
        redirectTo: '/'
    });
}]);
Run Code Online (Sandbox Code Playgroud)

controllers.js

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

    .controller('PostDetailController', ['$scope', '$http', '$routeParams', '$sce', function($scope, $http, $routeParams, $sce){
        $http.get('data/posts.json').success(function(response){
            $scope.post = response[$routeParams.id];
            console.log($routeParams.id);
            console.log($scope.post.id);
        });
    }])
Run Code Online (Sandbox Code Playgroud)

posts.html

<div class="container" id="postList">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div ng-repeat="post in posts | orderBy:post.date" class="post">
                <h2 class="blog-title"><a href="#/posts/{{posts.indexOf(post)}}">{{post.title}}</a></h2>
                <span class="date">Posted on {{post.date | 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>
                </div>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

singlepost.html

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

            <h3>{{post.headline}}</h3>
            <div class="postContent" ng-bind-html="post.content"></div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Luc*_*cio 3

您需要更改两个主要内容,首先是如何生成 URL,其次是如何恢复帖子。

从标题生成 URL

并非任何标题都可以作为 URL 有效,因此您需要使用slugs

视图中:

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

在控制器中:

$scope.getSlug = function(text){
  return text.replace(/\W+/g, '-');
};
Run Code Online (Sandbox Code Playgroud)

从 slug 搜索帖子

目前,您只是从数组中获取帖子作为索引,这在概念上是错误的(在删除一篇帖子后您会注意到这一点)。相反,通过数组进行搜索。如果您使用lodash ,这将非常简单。

像这样的事情会起作用:

$scope.post = _.find(response, function(post) {
  return post.title.replace(/\W+/g, '-') === $routeParams.id;
});
Run Code Online (Sandbox Code Playgroud)