简体   繁体   中英

How to store the data in this json format?

Given the following desired json api format:

 "type_of_advertisement":["ATM","Banner/Poster","Stalls"]

I have defined my ng-model like this:

.form-group
        .col-sm-offset-2.col-sm-10
          label.checkbox-inline
            input(type='checkbox', value='ATM', ng-model='type_of_advertisement')
            | ATM
          label.checkbox-inline
            input(type='checkbox', value='Mobile/Communication Tower', ng-model='type_of_advertisement')
            | Mobile/Communication Tower
          label.checkbox-inline
            input(type='checkbox', value='Banner/Poster', ng-model='type_of_advertisement')
            | Banner/Poster
          label.checkbox-inline
            input(type='checkbox', value='Hoarding Board', ng-model='type_of_advertisement')
            | Hoarding Board
          label.checkbox-inline
            input(type='checkbox', value='Stalls', ng-model='type_of_advertisement')
            | Stalls
          label.checkbox-inline
            input(type='checkbox', value='Digital Offline Marketing', ng-model='type_of_advertisement')
            | Digital Offline Marketing
          label.checkbox-inline
            input(type='checkbox', value='Area for Product Display', ng-model='type_of_advertisement')
            | Area for Product Display

and I have got the json format when I POST it on Angular having code is:

type_of_advertisement:[$scope.type_of_advertisement]

but I get the following instead:

"type_of_advertisement":["true"]

so what can I do to get the desired json api format?

You get the value of a checkbox. The checkbox is eather checked or unchecked (true or false). I don't know what you expect.

You must define a different model for each checkbox. And when you build your object, add a value in the array if the checkbox is checked.

I don't think there is an easier way to do this.

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