6
export class DuyurularPage {
    duyurular: any;
    loading: any;

    constructor(public navCtrl: NavController, public navParams: NavParams, public loadingPage: LoadingController,
        platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {

        this.loading = this.loadingPage.create({
            content: `
            <ion-spinner></ion-spinner>`
        });

        platform.ready().then(() => {
            statusBar.styleDefault();
            splashScreen.hide();

            var notificationOpenedCallback = function (jsonData) {
                this.duyurular = jsonData.notification.payload;
            };

            alert
            window["plugins"].OneSignal
            .startInit("12312412412412", "1231241212")
            .handleNotificationOpened(notificationOpenedCallback)
            .endInit();
        });
    }
}

var notificationOpenedCallback = function... ← Inside this function, I cannot access this.duyurular.

I have to write this array (duyurular) up from json. How can I do that?

aaron
  • 39,695
  • 6
  • 46
  • 102
Omsl
  • 192
  • 2
  • 15

1 Answers1

12

It's because this function below has the wrong scope.

    var notificationOpenedCallback = function(jsonData) {
      this.duyurular = jsonData.notification.payload;
    };

Change it to a fat arrow function or define the scope outside of this block:

Preferred option:

    var notificationOpenedCallback = (jsonData) =>  {
      this.duyurular = jsonData.notification.payload;
    };  

Outside the function option:

    const self = this;

    var notificationOpenedCallback = function(jsonData) {
      self.duyurular = jsonData.notification.payload;
    };     

As for building the array, the data will come in as a JSON object. You need to get the data you want for your component out of the JSON and use it to build the array. See this question for more advice on how to do that: How to convert JSON object to JavaScript array

Chris Sharp
  • 2,005
  • 1
  • 18
  • 32
  • Very, very much thanks :) But ı have one more question. – Omsl Nov 03 '17 at 12:25
  • I did as you said, I can alert () the incoming data, but do not get in the html page.

    {{item.title}}

    {{item.body}}
    – Omsl Nov 03 '17 at 12:29
  • The data will come in as a JSON object. You need to get the data you want for your component out of the JSON and use it to build the array. See this question for more advice on how to do that: https://stackoverflow.com/questions/14528385/how-to-convert-json-object-to-javascript-array – Chris Sharp Nov 03 '17 at 12:32
  • Thank u very much – Omsl Nov 03 '17 at 13:44