2017-07-19 82 views
1
export class HomePage { 
    public news:Object; 

    constructor(public navCtrl: NavController,public httpdata:HttpdataproviderProvider) { 
    this.news = {}; 
    this.getAllNews(); 
    } 
    getAllNews(){ 
    let url = 'some url'; 

    this.httpdata.httpPost(url,someData) //some custom provider 
    .then(function(data){ 
     console.log(this.news); 
     this.news = data; 
    }) 
    } 
} 

为什么我不能访问新闻对象或将数据分配给新闻。它显示“无法读取属性‘离子2无法读取未定义的属性'新闻'

+0

你的回调应该是箭头功能.. –

回答

0

变化的回调函数,这样的’不确定的”的消息,所以将举行的背景下(这)

this.httpdata.httpPost(url,someData) //some custom provider 
.then((data) => { 
    console.log(this.news); 
    this.news = data; 
}) 

,或者你可以存储喜欢的上下文的参照这

getAllNews(){ 
    let url = 'some url'; 
    let self = this; 

    this.httpdata.httpPost(url,someData) //some custom provider 
    .then(function(data){ 
    console.log(self.news); 
    self.news = data; 
    }) 
} 
+2

请不要回答重复的问题:https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – echonax

相关问题