2017-05-18 91 views
1

我使用這個代碼:Angular 2 http對象變量如何?

private ARRAYDATA: any[]; 
constructor(private http: Http) { 
    this.getCards('data.json'); 
} 
getCards(url) 
    { 
    this.http.get(url) 
       .map(response => response.json()) 
       .subscribe(
        function(response) { 
        console.log(response); //show object 
        }, 
        function(error) { console.log("Error happened" + error)}, 
        function() { console.log("the subscription is completed")} 
      ); 
    } 

此代碼的工作,現在我不知道如何傳遞一個對象響應變量ARRAYDATA;請幫幫我! 此代碼不起作用:

getCards(url) 
    { 
    this.http.get(url) 
       .map(response => response.json()) 
       .subscribe(
        function(response) { 
        console.log(response); //show object 
        this.arraydata = response; 
        }, 
        function(error) { console.log("Error happened" + error)}, 
        function() { console.log("the subscription is completed")} 
      ); 
       console.log(this.arraydata)// show undefind 
    } 

我需要使用一個變量ARRAYDATA功能之外。在這裏

constructor(private http: Http) { 
    this.getCards('data.json'); 
    console.log(this.ARRAYDATA); 
} 
+0

你需要在這樣的條件的時間或使用過濾器複製單個記錄它會過濾所有的數據[ – geminiousgoel

+0

可能的複製如何返回從可觀察/ HTTP /異步在angular2調用的響應?](http://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2) – Alex

+0

你不能訪問'ARRAYDATA'在構造函數裏面像這樣。就像您從副本中看到的一樣,數據僅在訂閱內可用。事情就是這樣,你無能爲力。您需要在收到回調後操作回調中的數據:) – Alex

回答

1

該請求發生異步。如果您需要操作結果,請移動代碼以在回調中使用該數據。

getCards(url) 
    { 
    this.http.get(url) 
       .map(response => response.json()) 
       .subscribe(
        function(response) { 
        console.log(response); //show object 
        this.arraydata = response; 
        console.log(this.arraydata)// use it here! 
        }, 
        function(error) { console.log("Error happened" + error)}, 
        function() { console.log("the subscription is completed")} 
      ); 
    } 

大量的角度發生異步。這意味着你需要做好準備,等待結果通過使用觀察結果或承諾返回。在這種情況下,您可能會更好地將變量保存在變量中,然後您可以在需要使用該值的任何位置訂閱observable。

+0

是的,它可以工作,但我需要在函數外使用一個變量, – Afimidas

+0

您通過暴露observable(或其修改後的版本),然後訂閱那個你需要價值的地方。 – Duncan

0

這是完全正常的。您的http get進行異步調用,它不會等待http.get的響應,並在訂閱後直接執行並執行console.log()

然後,響應將來自服務器,您將影響對arraydata的響應。

我強烈建議你看看javascript中的異步工作。

希望它有幫助。

2

您有兩個問題在這裏。

  1. HTTP響應返回觀測它是異步的,這意味着你可以得到內的數據訂閱功能。

  2. 您正在使用常規功能,這意味着您的this將更改並指向功能對象而不是類。您應該使用Arrow Functions進行回叫。 ()=>{}不會將函數對象分配給this

    this.http.get(url) 
         .map(response => response.json()) 
         .subscribe(
         (response) => { 
          console.log(response); //show object 
          this.arraydata = response; 
          console.log(this.arraydata); 
          }, 
         (error) => { console.log("Error happened" + error)}, 
         () => { console.log("the subscription is completed")} 
        ); 
    
+0

是的,它可以工作,但我需要在函數外部使用一個變量! – Afimidas

+0

你的意思是在模板中? 'this.arraydata'將會有值..只有一件事是它會被設置一旦返回響應 –

+0

我需要在函數外部使用一個變量ARRAYDATA。在這裏 構造函數(私有http:http){ this.getCards('data.json'); console.log(this.ARRAYDATA); } – Afimidas