简体   繁体   中英

I have a JSON with several fields and a key of keys, how can i by $resorce of angularjs bring only this key of Keys?

My Json is this

[
{
    "usuario": {
        "id": 2,
        "username": "licorera1"
    },
    "productos": [
        {
            "id": 2,
            "descripcion": "Botella",
            "precio": "1500000",
            "marca": {
                "nombre": "Red Label",
                "tipo": {
                    "descripcion": "Whiskey"
                }
            },
            "imagen": "http://192.168.0.12:8000/media/producto/redlabel.png"
        },
        {
            "id": 1,
            "descripcion": "Media Botella",
            "precio": "50000",
            "marca": {
                "nombre": "Nectar",
                "tipo": {
                    "descripcion": "Aguardiente"
                }
            },
            "imagen": "http://192.168.0.12:8000/media/producto/nectarrojo_qgCTYsx.jpg"
        }
    ]
}
]

and what i want to do is to get only de object "productos".

the entire url of the json is http://192.168.0.12:8000/api/productos_licorera/?pk=2&licorera=

so in my services.js i use the $resource to get de JSON

this is my services.js

(function (){

angular.module('productos.services',['ngResource'])

.factory('LicoresService',['$resource',function($resource){
return $resource('http://192.168.0.12:8000/api/productos_licorera/', {}, {
  query: {method:'GET', params:{pk:2,licorera:''}, isArray:true}
    });
 }]);
})();

and this is my controller.js

(function(){

angular.module('productos.controllers',[])
.controller('LicoresController',['$scope','$http', '$resource', 'LicoresService',function($scope, $http,$resource, LicoresService){
    var licores;
    var show = false; 
    $scope.licores = LicoresService.query();
}])
.controller('TabsController', function () {
  this.tab = 4;
  this.selectTab = function (tab) {
    this.tab = tab;
      console.log(this.tab);
  };
});    
})();

i need to get in the variable "licores" not all the Json, just only the "productos" object.

JSON is a 'transport' format. You can't transport a partial JSON.
Though, after you have parsed JSON to a regular javascript object, you can pick the key you like:

$scope.licores = JSON.parse(LicoresService.query())[0].licores;

Hope this can help you...

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