简体   繁体   中英

How to create key value pair in angularjs

I need to create a key value pair in the following format:

             // 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]'=>'',

I tried creating like this but I guess I am doing something wrong

       // 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"}
          ];

Any help would be appreciated. Thanks in advance.

This is what you should be doing:

$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"
};
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"
      }
    })
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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