how to pass parameter value to service function in angularjs controller? -


i have angularjs project on homepage want show several selected articles (objects) mongodb database. in database, have 2 collections -- 1 'articles' few hundred individual articles (objects) , other 'homepage' have ten objects 'articleid' key contains id of object first collection. problem when hardcode articleid in controller works fine when first fetch articleid 'homepage' collection , pass value function 'getarticle01' fetch selected object first collection not work. please, can let me know i'm doing wrong?

here code:

var articleid01 = '58da9967a8bccd763c48bdc0'; // if hardcode article id here, works fine  homepageservice    .findallhomepages() // returns array 10 objects api    .then(function(homepages) {      var articleid01 = homepages.data[0].articleid;      console.log(articleid01); // far, ok, console logs value: '58da9967a8bccd763c48bdc0'    }, function(error) {        console.log('error', error);    })    .then(function(){      function getarticle01() {        homepageservice          .findhomepagebyid(articleid01) // unfortunately, value not passed here          // .findhomepagebyid('58da9967a8bccd763c48bdc1') // if value hardcoded, ok          .then(function(response) {            console.log(response.data);          }, function(error) {              console.log('error', error); // here error console: object {data: null, status: -1, config: object, statustext: "", headers: function}          });      }        getarticle01();      });

the problem var articleid01 in scope of first then() function. not visible inside second then().
can if return articleid01 @ end of first then() , use parameter in second then():

homepageservice    .findallhomepages() // returns array 10 objects api    .then(function(homepages) {      var articleid01 = homepages.data[0].articleid;       //return id      return articleid01;    }, function(error) {        console.log('error', error);    })    //get id    .then(function(articleid){      function getarticle01() {        homepageservice        //use id          .findhomepagebyid(articleid)           .then(function(response) {            console.log(response.data);          }, function(error) {              console.log('error', error);           });      }        getarticle01();      });


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -