Getting a variable from .ts file to html file


#1

Hey guys I have a problem which I hope you guys can help me out. I have a variable in my .ts file and I need to bring it over to my html file. Do you guys know how? Thanks!

export class HomePage {

static myid: any;

  constructor(private platform: Platform) {

    HomePage.myid = 1000; <-- I need to bring this variable to my html file
  }
}

#2

.ts

export class HomePage {

public homePage : number = null;

  constructor(private platform: Platform) {

    this.homePage =  1000; <-- I need to bring this variable to my html file
  }
}

.html file

<ion-content>
{{homePage}}
</ion-content>

I strongly recommend you to read some tutorials about ionic 2. Josh Morony have some good ones.


#3

Hey thanks for the response! Will definitely check the link out. Is it possible to store {{homePage}} in a variable? Like var = {{homepage}} ?

I would like use it in this situation in my html file:

*ngIf="{{homePage}} == data.myid"

Thanks!


#4

This questions that you’re doing will be answered if you study some Angular 2 concepts & Syntax used on Ionic.

You can use the ngIf like

*ngIf="homePage == myid"

#5

homePage and myid are both bad names. I realize people are often simplifying things for posting purposes, but in actual code it is vital to give properties names that describe to an uninformed reader what the property’s purpose is. This will make your code self-documenting and maintainable.

Also, the property in OP is static, which won’t work. Templates can only bind to ordinary object properties.


#6

I agree with @rapropos, and also recommend the book ‘The Clean Coder: A Code of Conduct for Professional Programmers’ by Robert Cecil Martin.


#7

hey can you explain this?
from where you get “myid” because you written code for homepage variable=1000 so where placed myid.?