7
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?

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

4 Comments

Very, very much thanks :) But ı have one more question.
I did as you said, I can alert () the incoming data, but do not get in the html page. <ion-card class="card-background-page" *ngFor="let item of duyurular"> <ion-item> <ion-avatar item-start> <img src="./assets/img/avatar.png"> </ion-avatar> <h2>{{item.title}}</h2> </ion-item> <ion-card-content>{{item.body}}</ion-card-content> </ion-card>
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: stackoverflow.com/questions/14528385/…
Thank u very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.