Area variable access value inside callback function in Angular2

I am trying to extract data from a database and set it to a scope variable. This is inside a callback that does not work as an angular2 scope. I do not know why. I tried timeout zone. gas stuff, a promise. I don’t know what it is, so I can’t use it. Please help me figure this out.

  //in service.ts
  listFriends(callback) {
    result_data = [];

    db.transaction(function(tx) {

        tx.executeSql('SELECT * from mytable', [], function(tx, results) {

            length = results.rows.length;

            for (i = 0; i < length; i++) {

                //console.log(results.rows.item(i));
                result_data.push(results.rows.item(i))
            }
            callback(result_data);

        }, null);

    });

}
//in component.ts
public allmyFriends: any;
public self = this;
public test;
constructor(myFriendService: MyFriendService) {



    this.myFriendService = myFriendService;
    this.myFriendService.listFriends((response) => {
        //trying to set value to the scope variable.but not working
        this.test="test working";
        //same as above
        this.allmyFriends = response;
        //getting the response from the service successfully
        console.log("in list" + this.allmyFriends);

    } );
  }
+4
source share
1 answer

When you said you tried the zone. What do you specifically mean? You enter NgZone and execute the code inside it.

: Angular2.

. , Angular2 mapLatitudeInput.

- :

export class MapComponent {
  constructor(ngZone:NgZone) {
    this.ngZone = ngZone;
  }

  someMethod() {
    this.myFriendService.listFriends((response) => {
      this.ngZone.run(() => {
        this.test="test working";
        this.allmyFriends = response;
        console.log("in list" + this.allmyFriends);
      });
    });
  }

: Angular2 , .

, , Thierry

+4

Source: https://habr.com/ru/post/1624752/


All Articles