# Pass variable to view from construct function?

**URL:** <https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788>\
**Category:** ionic-v3\
**Created:** [August 21, 2017, 9:43pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788 "2017-08-21T21:43:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wforce](https://avatars.discourse-cdn.com/v4/letter/w/9e8a1a/32.png) [@wforce](https://forum.ionicframework.com/u/wforce)\
**Post date:** [August 21, 2017, 9:43pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/1 "2017-08-21T21:43:18Z")

</div>

I call in my app construct() function a external json url, in console log I see correct result of:

```auto
var url = 'https://my_path.co/apiv1/products';
this.http.get(url).map(res => res.json()).subscribe(items => console.log(items));

```

How can I pass this result to my view and render with _\*ngFor_?

---

<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:** [August 21, 2017, 9:44pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/2 "2017-08-21T21:44:55Z")

</div>

> [@wforce](#):
>
> I call in my app construct() function a external json url

Not a good place for this. Do it instead in an injectable service provider’s constructor, so your question becomes simple: just inject the provider in whatever page needs to display the information.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 21, 2017, 9:59pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/3 "2017-08-21T21:59:40Z")

</div>

Don’t do anything hard in a constructor. It slows down your page rendering. Also, you have access to ES6, so it’s much better to use let, and stop using var. Putting the work in a provider is the best approach to scale. But to give you an idea, here is a not-very-good way to do this:

```auto
const url = 'https://my_path.co/apiv1/products';
products = Observable<whateverTypeTheProductListIs[]> = new Observable<whateverTypeTheProductListIs[]>();

constructor() {
  products = this.http.get(url).map(res => res.json());
}

```

And in the template, use `products|async` .

---

<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:** [August 21, 2017, 10:08pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/4 "2017-08-21T22:08:53Z")

</div>

> [@AaronSterling](#):
>
> Don’t do anything hard in a constructor.

I wouldn’t classify firing off an asynchronous HTTP request “hard”.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 21, 2017, 10:10pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/5 "2017-08-21T22:10:22Z")

</div>

Ha, I more or less believe, “Don’t do anything at all in a constructor!”

---

<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:** [August 21, 2017, 10:18pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/6 "2017-08-21T22:18:07Z")

</div>

This is one situation where I feel a service provider constructor is absolutely the right place: where you are initiating a fetch for data from an external source that you want to be available throughout the app as soon as reasonably possible. I don’t think it introduces any rendering overhead, as we are off the main thread almost immediately.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 21, 2017, 10:22pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/7 "2017-08-21T22:22:18Z")

</div>

Yeah, I should change my not-completely-silly rule to, “Do nothing in a page constructor.” I do things in provider constructors because why not, that doesn’t slow down page rendering. Though (for the reader who does not know) the constructor is run the first time the provider is injected and used, so if you have a heavy operation that only some pages use, it’s probably better to keep it in a doHeavyOperation() method, instead of the constructor, because otherwise a page that doesn’t need the heavy operation might pay for it anyway.

---

<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:** [August 21, 2017, 10:24pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/8 "2017-08-21T22:24:47Z")

</div>

> [@AaronSterling](#):
>
> Do nothing in a page constructor

A proposed exception: putting reactive forms together using `FormBuilder`. I used to try delaying that to various lifecycle handlers (notably `ngOnInit` and `ionViewDidLoad`) and encountered more trouble than I considered it worth.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 21, 2017, 10:34pm UTC](https://forum.ionicframework.com/t/pass-variable-to-view-from-construct-function/102788/9 "2017-08-21T22:34:04Z")

</div>

I had the same experience. I can’t help thinking there’s something I missed, but I broke down and put the FormGroups in a constructor. (Well, not exactly true, I put a call to refreshForm() in the constructor, and define a custom form refresher that I can call from other methods. But it’s the same idea.)
