Save json in an angular model1 (ionic1)

2

I have a query in angular. I need to access the data of a json

[
  {
    "gift": [
      {
        "description": "Recibe un pichel de te gratis!!",
        "name": "Te para Todos",
        "cod": "Te01",
        "poll": 6,
        "id": 5
      }
    ],
    "question": [
      {
        "header": "Que tan seguido viene a nuestro local?",
        "poll": 6,
        "id": 16
      },
      {
        "header": "quisiera mas promociones?",
        "poll": 6,
        "id": 17
      }
    ],
    "name": "PH01",
    "id": 6
  }
]

mainly I do not know how to access the header within question, the amount of question is variable in all queries. my model is this:

angular.module( 'pollmodel', [] )

.factory( 'Poll', function () {
    function Poll( name, ) {

    }
    Poll.build = function ( data ) {
        if( !data ) {
            return null;
        }
        return new Poll( data.name )
    }

    Poll.prototype.toJson = function () {
        return angular.toJson( this );
    };

    Poll.fromJsonBunch = function ( data ) {
        if( angular.isArray( data ) ) {
            return data.map( film.build ).filter( boolean );
        }
        return film.build( data );
    }

    return Poll;
} )

is incomplete. because I have not known how to complete it so that it properly receives the json.

    
asked by bisanbl 17.08.2016 в 19:41
source

1 answer

1

You need to make a map to get the header fields.

var datos = [{
"gift": [
  {
    "description": "Recibe un pichel de te gratis!!",
    "name": "Te para Todos",
    "cod": "Te01",
    "poll": 6,
    "id": 5
  }
],
"question": [
  {
    "header": "Que tan seguido viene a nuestro local?",
    "poll": 6,
    "id": 16
  },
  {
    "header": "quisiera mas promociones?",
    "poll": 6,
    "id": 17
  }
],
"name": "PH01",
"id": 6 }];

// Sacar los headers de las questions, crea un array con los headers
var headers = datos[0].question.map(function(q) {
                    return q.header; //devolver el campo header de cada question
                });

And if there were more than one element in the array you would have to do the same to get the multiple questions with a map.

    
answered by 18.08.2016 в 10:07