Using .ts file variables in .html file


I have a jsonp data source and the response looks like below:


My controller (microsite.ts) look like this:

import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import { Http, Jsonp } from ‘@angular/http’;

selector: ‘page-microsite’,
templateUrl: ‘microsite.html’
export class MicrositePage {

public micdetails: any;

constructor(public navCtrl: NavController, private http: Http, public jsonp: Jsonp) {

	res => {
    this.micdetails = res.json();



When I try to console.log the variable it works well, but when I try to use the same variable in template file (microsite.html) like:

Microsite {{}}

I get error as:

I am unable to understand, how can I use controller variables in template file.

That’s because of asynchronicity. As you can see in your log the error occurs before the object get’s logged. Which means you’re binding in your html to something that doesn’t exist when rendering the component (i.e. {{}}) . Make sure micdetails is loaded before rendering the HTML or initialize micdetails with empty settings in your class.

Easiest solution is to *ngIf="!!micdetails" on the top of the specific micdetails item. This makes sure micdetails isn’t empty so Angular won’t try and render it when it isn’t available yet.

1 Like