【问题标题】:push method in Array not working - Angular 2数组中的推送方法不起作用 - Angular 2
【发布时间】:2016-09-11 16:42:18
【问题描述】:

在过去的 4 个小时里,我一直被这段代码所困扰,无法弄清楚问题所在。它是用打字稿写的。 newsa 数组将填充我使用 api 提取的数据。但相反,它显示像 can't find push property 之类的错误,在我使用公共前缀等所有需求正确声明数组之后。没有错误,但没有任何内容被推送。怎么了。 如果您回答将有很大帮助。

public newsa: Array<any> = [];
fetchNews() {
    console.log("Fetch NEws has started");
    var that = this;
    let googlenewsUrl:string;
    let finalUrl: string;

    this.countries.forEach(function(i) {

        googlenewsUrl = "https://news.google.com/news?q=" + i.name.replace(/\s+/g, '') + "&output=rss";
        finalUrl = '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=50&callback=JSONP_CALLBACK&q=' + encodeURIComponent(googlenewsUrl);
        that.jsonp.get(finalUrl).toPromise().then(news => that.newsa.push(news)).catch(that.handleError);           
    });

}

【问题讨论】:

  • api 请求也是成功的。除了主要推动部分外,一切都运行良好。
  • 您应该用您的解决方案回答您自己的问题。 Relevant xkcd.
  • @HrishikeshBarman 如果你想通了,你必须在下面添加你的答案,并接受它。

标签: arrays angularjs api angular promise


【解决方案1】:

而不是

 function(i)

使用

 (i) =>

保留this的范围

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    相关资源
    最近更新 更多