承诺的角度离子

时间:2014-07-21 22:19:40

标签: angularjs ionic-framework

我是离子框架和承诺的新手,这是我工厂的一部分,Productos

...
    getCombinations: function(productId) {
            var promises = [];
            // todo: quizas filtrar por actividad
            url_to = 'http://example.com/api/combinations?display=full&filter[id_product]=' + productId;
            $http({method: 'GET', url: url_to })
                    .then(function(response) {
           results = x2js.xml_str2json(response.data);
           angular.forEach(results.prestashop.combinations.combination, function(val, key) {
                               var deferred = $q.defer();
                               console.log('first deferred');
                               console.log(deferred);
               url_to = 'http://example.com/api/product_option_values?display=[name,color]' + '&filter[id]=[' + val.associations.product_op
    tion_values.product_option_value[0].id + '|' + val.associations.product_option_values.product_option_value[1].id + ']';
               $http({method: 'GET', url: url_to }).then(function(response) {
                                    results = x2js.xml_str2json(response.data);
                                    resultt = {'size': results.prestashop.product_option_values.product_option_value[0].name.language.__cda
    ta, 'color_hex':results.prestashop.product_option_values.product_option_value[1].color.__cdata, 'color_name': results.prestashop.product_option_values.product_option_value[1].name.language.__cdata}; 
                                    deferred.resolve(resultt); 
               })
               promises.push(deferred);
               console.log('pushed');
               console.log(promises);
           });
       }, function(response) {
               return $q.reject(response.data);
               //algo fue mal
       });

     return $q.all(promises);

      },
    ...

并在我的控制器中

...
          Productos.getCombinations(productId) 
                    .then(function(data) {    
            console.log('getcombinations');
            console.log(data);
...

但是我的数据是空的,有人可以帮助我,提前谢谢。

1 个答案:

答案 0 :(得分:-1)

这有助于弄清楚如何做到这一点:

https://egghead.io/lessons/angularjs-chained-promises