如何在angularjs中创建键值对

时间:2018-02-03 19:44:21

标签: angularjs

我需要按以下格式创建一个键值对:

             // KEY           VALUE //

            'order[email]'=>'demo@user.com',
            'order[shipping_method]'=>'flatrate_flatrate',
            'order[payment_method]'=>'checkmo',
            'billing[entity_id]'=>1,
            'billing[street]'=>'1959 Settlers Lane',
            'billing[city]'=>'Culver City',
            'billing[country_id]'=>'US',
            'billing[region]'=>'California',
            'shipping[entity_id]'=>1,
            'shipping[first_name]'=>'John',
            'shipping[middle_name]'=>'A',
            'shipping[last_name]'=>'Deo',
            'shipping[company]'=>'',

我试过像这样创作,但我想我做错了什么

       // key value pair 

       $scope.list = [
                { 'order[email]' : "akashkt09@gmail.com" },
                { 'order[shipping_method]': "flatrate_flatrate"},
                { 'order[payment_method]':"checkmo"},
                { 'billing[entity_id]':"3"},
                { 'billing[street]':"street10"},
                { 'billing[city]':"GuangZhou"},
                { 'billing[country_id]':"IN" },
                { 'billing[region]':null},
                { 'billing[region_id]':"0"},
                { 'billing[postcode]':"834005"},
                { 'billing[telephone]':"8860052373"},
                { 'billing[fax]':null},
                { 'shipping[entity_id]':"3"},
                { 'shipping[street]':"street10"},
                { 'shipping[city]':"GuangZhou"},
                { 'shipping[country_id]':"IN" },
                { 'shipping[region]':null},
                { 'shipping[region_id]':"0"},
                { 'shipping[postcode]':"834005"},
                { 'shipping[telephone]':"8860052373"},
                { 'shipping[fax]':null},
                { 'shipping[first_name]':"Akash"},
                { 'shipping[last_name]':"Kumar"}
          ];

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

这就是你应该做的事情:

$scope.list = {
    'order[email]': "akashkt09@gmail.com",
    'order[shipping_method]': "flatrate_flatrate",
    'order[payment_method]': "checkmo",
    'billing[entity_id]': "3",
    ...
    'shipping[first_name]': "Akash",
    'shipping[last_name]': "Kumar"
};

答案 1 :(得分:0)

Try this :

<div ng-app>
  <div ng-controller="TodoCtrl">
   <div ng-repeat="item in list">
     {{item}}
   </div>
</div>


function TodoCtrl($scope) {

    function createItem(data){
    return {
        order:{
        email: data.order.email,
        shipping_method:data.order.shipping_method,
        payment_method:data.order.payment_method
      },
      billing:{
        entity_id: data.billing.entity_id,
        street:data.billing.street,
        city:data.billing.city,
        country_id:data.billing.country_id,
        region:data.billing.region
      },
      shipping:{
        entity_id:data.shipping.entity_id,
        first_name:data.shipping.first_name,
        middle_name:data.shipping.middle_name,
        last_name:data.shipping.last_name,
        company:data.shipping.company
      }
    }
  }
  $scope.list = [
    createItem({
        order:{
        email:"test@test.com",
        shipping_method:"Post",
        payment_method:"Cash"
      },
      billing:{
        entity_id:1,
        street:"test",
        city:'test',
        country_id:1,
        region:"test"
      },
      shipping:{
        entity_id:1,
        first_name:"test",
        middle_name:"test",
        last_name:"test",
        company:"test"
      }
    })
  ]
}