search

Home  >  Q&A  >  body text

angular.js - angularjs url passes an object

Previously, the pass parameters were in the form of multiple key:value. The current requirement is to pass the entire object, like the following

Then my writing method is

                 /*前往订单详情页面*/
                $scope.goOrderDetails=function(){
                //counArr就是图片里的对象
                    $state.go('ticket.pay',{countArr:countArr})
                } 
                

On the receiving page

var countArr=$stateParams.countArr;
                console.log(countArr);
                console.log(countArr.length);

The result is wrong, the result of console.log(countArr) is [object object],
url also becomes 127.0.0.1:9002/ticket/pay/[object Object]
I think I know what the problem is. I have never encountered the need to pass an object before

给我你的怀抱给我你的怀抱2855 days ago838

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-15 17:08:33

    If you want to spread it like this, you have to

    $state.go('ticket.pay',{countArr: JSON.stringify(countArr)})

    When picking up

    var countArr = JSON.parse($stateParams.countArr)

    If you want to transfer the object directly, it is recommended to use browser storage to transfer it. Please refer to this

    reply
    0
  • 阿神

    阿神2017-05-15 17:08:33

    Do I need to convert it to json first?

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:08:33

    Set params in routing
    . state('urlname',{

    url:'urlname',
    templateUrl:...,
    controller:...,
    params:{testObj:""}

    })

    In this way, the testObj taken out from the page is still an object, just use the attributes you need directly

    Supplementary. . . Of course, this params can also be testObj:{}

    reply
    0
  • Cancelreply