我使用這個代碼: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);
}
你需要在這樣的條件的時間或使用過濾器複製單個記錄它會過濾所有的數據[ – geminiousgoel
可能的複製如何返回從可觀察/ HTTP /異步在angular2調用的響應?](http://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2) – Alex
你不能訪問'ARRAYDATA'在構造函數裏面像這樣。就像您從副本中看到的一樣,數據僅在訂閱內可用。事情就是這樣,你無能爲力。您需要在收到回調後操作回調中的數據:) – Alex