简体   繁体   English

如何通过URL传递带有对象值的参数

[英]How to pass parameters with Object Values through URL

I created a project that allows converting HTML to PDF. 我创建了一个项目,该项目允许将HTML转换为PDF。 I'm using phantom-render-stream to convert the HTML to PDF. 我正在使用phantom-render-stream将HTML转换为PDF。 My problem now, I failed to pass an object value to HTML. 现在的问题是,我无法将对象值传递给HTML。 Below is my code. 下面是我的代码。

app.js app.js

var username = "AAA";
var pets = [
   {id: 1, name: "cat"},
   {id: 2, name: "dog"},
   {id: 3, name: "sugar glider"}
];
var address = {
    line1: "Street A123",
    town: "Edinburgh",
    country: "Scotland"
};

render('http://localhost:8888/createForm.html?username='+username+'&pets='+pets+'&address='+address
.pipe(fs.createWriteStream('customer.pdf'));

In createForm.html , I used an AngularJS to get and view the value to PDF. createForm.html ,我使用了AngularJS来获取和查看PDF的值。

createForm.html createForm.html

<script>
        var app = angular.module('myApp', []);

        app.config(function($locationProvider){
            $locationProvider.html5Mode(true);
        });

        app.controller('getDataCtrl', function($scope, $location) {
                         var url = decodeURIComponent( $location.url() );
             var value = $location.search();

            $scope.username = value.username;
            $scope.pets = value.pets;
                       $scope.address = value.address;
            })
</script>

<body>
      <div ng-app="onePayForm" ng-controller="getDataCtrl">
           <p>{{username}}</p>
           <p>{{pets[0].name}}</p>
           <p>{{address.line1}}</p>
      </div>
</body>

After successfully converting HTML to PDF, I opened the pdf file to see the result. 将HTML成功转换为PDF后,我打开了pdf文件以查看结果。 Only username appears in pdf, the rest shows like this {{pets[0].name}} {{address.line1}} . pdf仅显示用户名,其余显示为{{pets[0].name}} {{address.line1}}

in

render('http://localhost:8888/createForm.htmlusername='+username+'&pets='+pets+'&address='+address)
    .pipe(fs.createWriteStream('customer.pdf'));

your query params need to be prepared before appending them to the url. 您需要在将查询参数附加到url之前进行准备。 What you should do 你应该怎么做

var readyPets = encodeURIComponent(JSON.stringify(pets));
var readyAddress = encodeURIComponent(JSON.stringify(address));

then change the code above to : 然后将上面的代码更改为:

render('http://localhost:8888/createForm.htmlusername='+username+'&pets='+readyPets+'&address='+readyAddress)
        .pipe(fs.createWriteStream('customer.pdf'));

in createForm.html parse these param queries: 在createForm.html中解析以下参数查询:

$scope.pets = JSON.parse(decodeURIComponent(value.pets));
$scope.address = JSON.parse(decodeURIComponent(value.address));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM