2017-07-19 81 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

相關問題