2017-04-06 67 views
0

我有angularjs項目,在主頁上我想從我的mongodb數據庫中顯示幾個選定的文章(對象)。在數據庫中,我有兩個集合 - 一個是包含幾百個單獨文章(對象)的「文章」,另一個是「主頁」,其中我只有十個對象,只有'articleID'鍵,其中包含對象的ID第一次收集。我的問題是,當我硬編碼控制器中的articleID所有工作正常,但是當我首先從'homepage'集合中獲取articleID,然後將該值傳遞給從第一個集合中獲取選定對象的函數'getArticle01'它不起作用。請,任何人都可以讓我知道我做錯了什麼?如何在angularjs控制器中將參數值傳遞給服務函數?

這裏是我的代碼:

var articleId01 = '58da9967a8bccd763c48bdc0'; // if i hardcode article id here, all works fine 
 
HomepageService 
 
    .findAllHomepages() // returns array with 10 objects from my api 
 
    .then(function(homepages) { 
 
    var articleId01 = homepages.data[0].articleID; 
 
    console.log(articleId01); // so far, all is ok, console logs value: '58da9967a8bccd763c48bdc0' 
 
    }, function(error) { 
 
     console.log('error', error); 
 
    }) 
 
    .then(function(){ 
 
    function getArticle01() { 
 
     HomepageService 
 
     .findHomepageById(articleId01) // unfortunately, value is not passed here 
 
     // .findHomepageById('58da9967a8bccd763c48bdc1') // if value is hardcoded, all is ok 
 
     .then(function(response) { 
 
      console.log(response.data); 
 
     }, function(error) { 
 
      console.log('error', error); // here is error from console: Object {data: null, status: -1, config: Object, statusText: "", headers: function} 
 
     }); 
 
    } 
 

 
    getArticle01(); 
 

 
    });

+0

您重新定義articleId01這裏:VAR articleId01 =主頁。數據[0] .articleID;所以當你在另一個函數中需要它時,它不存在。也許使用範圍變量? – Groben

回答

0

的問題是,var articleId01在第一then()功能的範圍。它在第二個then()內不可見。
你能得到它,如果你在你的第一個then()的最後返回articleId01並在第二then()使用它作爲一個參數:

HomepageService 
 
    .findAllHomepages() // returns array with 10 objects from my api 
 
    .then(function(homepages) { 
 
    var articleId01 = homepages.data[0].articleID; 
 
    //Return your id 
 
    return articleId01; 
 
    }, function(error) { 
 
     console.log('error', error); 
 
    }) 
 
    //Get your id 
 
    .then(function(articleId){ 
 
    function getArticle01() { 
 
     HomepageService 
 
     //Use the id 
 
     .findHomepageById(articleId) 
 
     .then(function(response) { 
 
      console.log(response.data); 
 
     }, function(error) { 
 
      console.log('error', error); 
 
     }); 
 
    } 
 

 
    getArticle01(); 
 

 
    });

+0

非常感謝你,但它沒有奏效。 'var articleId01'在第二個範圍內仍然不可見。我已經嘗試過相同的方法,但它不起作用,我真的不明白爲什麼。不過,非常感謝你的建議... – user7827012

+0

也許我誤會了你,但是你還想在第二個.then()中獲得'var articleId01'嗎?在我的例子中,這個變量作爲'articleId'傳遞給第二個.then(),所以它是可見的。 :)請參閱:'.findHomepageById(articleId)' – flow3r

+1

嗨,對於遲到的答覆抱歉,但你絕對是100%的權利。這是我的代碼中的另一個錯誤,「導致我的應用程序崩潰,並且我沒有注意到它,因爲我已經在stackowerflow編輯器中編寫了此代碼段,並且未從我的JavaScript文件中複製 - 因此此處的代碼是正確且相同的代碼在我的應用程序中有額外的錯誤... :-(再次,非常感謝你,這麼多你的幫助! – user7827012

0

你能做些什麼,和它的作品對我真的很好是建立一個getter/setter方法爲這個ID您的服務中。

您的服務:

return { 
    setId: setId, 
    getId: getId, 
...} 

    function setId(str) { 
     idStr = str; 
    } 
    function getId() { 
     return idStr; 
    } 

然後,你可以調用服務之前設置:

HomepageService.setId('your_id'); 

而且你的服務中,你可以這樣調用它:

var myId = getId(); // and use it 

其真的很有用,並保持我的代碼非常乾淨。我希望它有幫助。 你可以在這裏做了很多與他們一些更多的信息:

Getters- Setters

相關問題