2017-05-18 90 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