# templateUrl display before data loaded

**URL:** <https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510>\
**Category:** ionic-v3\
**Created:** [September 26, 2016, 7:38am UTC](https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510 "2016-09-26T07:38:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardmarais](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardmarais/32/101456_2.png) [@richardmarais](https://forum.ionicframework.com/u/richardmarais)\
**Post date:** [September 26, 2016, 7:38am UTC](https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510/1 "2016-09-26T07:38:52Z")

</div>

Hi All,

I have an Ionic 2 `Component`. It renders a html page. In the constructor, it fetches data in a promise. The html uses the data (`personModel`) and displays the values.

My problem is the html wants to render before the promise has completed getting the data, resulting in an error.

> `TypeError: self.context.personModel is undefined`

How do I make sure the html waits for the data to load before it tries to render?

Thanks

**html**

`<h2>{{personModel.firstName}}&nbsp;{{personModel.lastName}}</h2>`

**ts**

```
@Component({
  templateUrl: 'build/pages/person/person.html',
})

constructor() {
    // promise that loads data
    this.utilityService.getLoggedInPerson().then((data: string) => {
       this.personModel = JSON.parse(data);
    }
}

```

---

<div class="post-metadata">

**Author:** ![joshmorony](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/joshmorony/32/117695_2.png) [@joshmorony](https://forum.ionicframework.com/u/joshmorony)\
**Post date:** [September 26, 2016, 7:48am UTC](https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510/2 "2016-09-26T07:48:11Z")

</div>

You need to define the member variable straight away, and then assign the data to it, i.e:

```auto
personModel: any;

constructor() {
    // promise that loads data
    this.utilityService.getLoggedInPerson().then((data: string) => {
       this.personModel = JSON.parse(data);
    }
}

```

---

<div class="post-metadata">

**Author:** ![skydive](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@skydive](https://forum.ionicframework.com/u/skydive)\
**Post date:** [November 3, 2016, 9:08pm UTC](https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510/3 "2016-11-03T21:08:27Z")

</div>

It doesn’t work in my case: personalModel will be initialized after template is rendered.

Does anybody have different solution?

---

<div class="post-metadata">

**Author:** ![mjwdoek](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@mjwdoek](https://forum.ionicframework.com/u/mjwdoek)\
**Post date:** [April 12, 2017, 4:36am UTC](https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510/4 "2017-04-12T04:36:37Z")

</div>

At first I had the member defined without a type (so just personModel; ) You have to explicitly give the personModel a type of any. When I did that, it worked right away.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [April 12, 2017, 5:20am UTC](https://forum.ionicframework.com/t/templateurl-display-before-data-loaded/64510/5 "2017-04-12T05:20:39Z")

</div>

I completely disagree with all the recommendations of using `any` here. Type your properties properly and initialize them to viable dummy values. Moreover, the post above this is inaccurate. “I used `any` because it worked” is a weak justification in the first place, but in situations like that described by OP, it doesn’t even work. Templates don’t care about typing, and giving `personModel` a type of `any` would not fix the original problem. Assigning an empty object to it (`personModel = {}`) would.
