【问题标题】:Pass datas between routes angularjs/expressjs在路由 angular js/express js 之间传递数据
【发布时间】:2016-03-21 12:12:45
【问题描述】:

我有一个表单,当我提交它时,会显示另一个表单,其中包含其他数据的单选按钮。我选择一个并提交。我希望数据显示在另一个页面中,但它没有显示我所做的任何内容。

所以有 /form 路由,我想在上次提交后用我的数据重定向到 /confirmation 路由。

Bellow 是我在 angularjs 中的 formController

.controller('formController', ['$scope', '$http', '$location', function($scope, $http, $location) {
    $scope.pageClass = 'form';
    $scope.firstName = '';
    $scope.lastName = '';
    $scope.email = '';
    $scope.departure = '';
    $scope.destination = '';
    $scope.submitFirstForm = function() {
        $http.post('/form', {
            firstName: $scope.firstName,
            lastName: $scope.lastName,
            email: $scope.email,
            departure: $scope.departure,
            destination: $scope.destination,
        }).then(function(res) {
            $scope.response = res.data;
            });
        }

    $scope.submitConfirmationForm = function(flight){

        $http.post('/confirmation', {
            departure: $scope.departure,
            destination: $scope.destination
        }).then(function(res) {
            $scope.otherResponse = res.data;
            $location.path("/confirmation");
            });

    };
}]);

Bellow 是我的特快专递功能,仅适用于 /CONFIRMATION 路线

    app.post("/confirmation",function(req,res)
{
    var departure=req.body.departure;
    var destination=req.body.destination;

    otherResponse = {
        "confirmation": [{
            "departure": departure,
            "destination": destination,
            "msg": "MSG OK",
        }]
    };

    res.json(otherResponse);
});

最后,在我的 Confirmation.html 中查看我的 json otherReponse 发生了什么

 <pre>
    {{otherResponse | json}}
</pre>

【问题讨论】:

    标签: javascript angularjs forms express


    【解决方案1】:

    你没有传递“价值”,你需要做这样的事情:

    $location.path('/confirm/').search({param: 'otherResponse'});

    $location 方法是可链接的。

    这个产品:

    /confirm/?param=otherResponse

    查看$location 的文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多