路由器内的MEAN堆栈传递参数

时间:2015-04-12 18:55:15

标签: angularjs node.js mongodb express mean

我是MEAN堆栈开发的新手。尝试按照本教程https://thinkster.io/mean-stack-tutorial/来使用简单的Web应用程序。我在下面发布了我的代码。我创建了一个名为post的中间层参数(请参阅router.js文件)以获取特定帖子。在我的postCtrl中,我想将post / postId传递给工厂并获取特定帖子。        //$scope.post = postFactory.getById(id); 根据教程,应该从URL路由自动检测帖子。所以我想知道如何利用它来获得我想要的帖子?感谢您提前的时间

AngularController.js

var app = angular.module("littleStar" , ["service", "ui.router"]);
app.config([
    '$stateProvider',
    '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider) {

        $stateProvider
            .state('home', {
                url: '/home',
                templateUrl: "/home.html",
                controller: 'mainCtrl'
            })
            .state('post', {
                url:'/post/{id}',
                templateUrl: '/post.html',
                controller:'postCtrl'
            });

        $urlRouterProvider.otherwise('home');
    }]);


app.controller("mainCtrl", ["$scope", "$http", "postFactory", function ($scope, $http, postFactory) {
    postFactory.get().success(function(data){
        $scope.posts = data;
    });

    $scope.addPost = function() {
        var title = $scope.title;
        var link = $scope.link;
        if (!title || title === "" || !link || link === "") {
            return;
        }
        var newPost = {
            "title": title,
            "link": link
        }
        postFactory.create(newPost)
            .success(function (data) {

                postFactory.get().success(function(allPosts){
                    $scope.posts = allPosts;


                });

            });
        $scope.title = "";
        $scope.link = "";

    };

    $scope.incrementPost = function(post){
        post.upvotes += 1;
    };
}]);


app.controller("postCtrl", ["$scope", '$stateParams', "postFactory", function($scope, $stateParams, postFactory){
    //$scope.post = postFactory.get($stateParams.id);
    //$scope.post = postFactory.getById($stateParams.id);

    $scope.addComment = function(){
        var currentComments = postFactory.post[$stateParams.id].comments;
        currentComments.push({
            author:$scope.author,
            body: $scope.body,
            upvotes: 0
        });

        $scope.body = "";
    }

    $scope.incrementComment = function(comment){
        comment.upvotes += 1;

    }
}]);

    router.get('/posts/:post', function(req, res) {
        res.json(req.post);
    });

    router.param('post', function(req, res, next, id) {

        var query = Post.findById(id);

        query.exec(function (err, post){
            if (err) { return next(err); }
            if (!post) { return next(new Error('can\'t find post')); }
            console(id);
            req.post = post;
            return next();
        });
    });

angularService.js

    var service = angular.module("service", []);

    service.factory("postFactory",  ["$http", function($http){
            return {
                get : function(){
                    return $http.get("/posts");
                },
                create: function(newPost){
                    return $http.post("/post", newPost);
                },
                delete : function(id){
                    return $http.delete("/post/" + id);
                },
                getById : function(id){
                    return $http.get("posts/" + id);
                }
            }


    }]);

1 个答案:

答案 0 :(得分:0)

您应该能够通过以下方式制作邮寄路线:

  router.get('/posts/:post', function(req, res) {
        var query = Post.findById(req.params.post);

        query.exec(function (err, post){
            res.json(post);
        });
    });