mark200
mark200

Reputation: 47

How to display data from function in component nativescript?

How to display data from function in component ? My function is:

getStrings() {
    let myString:any;
      http.getString("https://projectzenith.pl/testy/send.php").then((r:string) => {
         myString.set("getStringResult" ,r );
      },(e) =>{});
      return myString;
  }

I would like display myString in something like this:

<StackLayout>
<Label text="My new string">
</Label>
<TextView text=["myString"]></TextView>
</StackLayout> 

But I see error: Identifier "myString" is not defined.

Upvotes: 0

Views: 54

Answers (1)

Ian MacDonald
Ian MacDonald

Reputation: 14030

Your getStrings function is calling an async function but returning immediately without guaranteeing the result has been stored in myString first. Your template is referencing a function-local variable instead of an instance-local variable (or the function itself). There are a number of ways that you could solve these issues. The following offers the least impact to your source, but provides a significant chance of synchronization bugs.

  public myString: string;

  constructor() {
    this.getStrings();
  }

  getStrings() {
    http.getString("https://projectzenith.pl/testy/send.php").then((r: string) => this.myString = r);
  }
<Label [text]="myString"></Label>

Upvotes: 1

Related Questions