2

I am learning Angular 2 (with Ionic 2) - there are two concepts which I cannot relate to from Angular 1.

I have a class like follows:

import {App, Platform} from 'ionic-angular';
import {RegisterPage} from './pages/register/register';


@App({
    templateUrl: 'build/index.html',
    config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
    static get parameters() {
        return [[Platform]];
    }

    constructor(platform) {
        this.rootPage = RegisterPage;

        platform.ready().then(() => {
            // The platform is now ready. Note: if this callback fails to fire, follow
            // the Troubleshooting guide for a number of possible solutions:
            //
            // Okay, so the platform is ready and our plugins are available.
            // Here you can do any higher level native things you might need.
            //
            // First, let's hide the keyboard accessory bar (only works natively) since
            // that's a better default:
            //
            // Keyboard.setAccessoryBarVisible(false);
            //
            // For example, we might change the StatusBar color. This one below is
            // good for dark backgrounds and light text:
            StatusBar.setStyle(StatusBar.LIGHT_CONTENT);
        });
    }
}

What is a provider and what does it do/what is its purpose?

What does the following code do:

static get parameters() {
    return [[Platform]];
}
Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
keldar
  • 6,152
  • 10
  • 52
  • 82

1 Answers1

3

Since you use ES6, you don't have parameter types. The following isn't possible (only TypeScript):

export class MyApp {
  constructor(platform:Platform) {
  }
}

Using the static getter you will configure the types of parameters to provide to the constructor. Angular2 dependency injection will leverage this to know which providers to use to inject the parameters of the constructor. It read the content of the parameters property for the class...

Using the getter you define the value of this "static" property.

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • That's a great answer - thanks very much; they're arrays in an array... can the inner-array contain more than one item or is at most of length 1? – keldar Mar 10 '16 at 16:42
  • 1
    You're welcome! I don't know why there is an array into an array. But for sure, you can have several elements in it if you expect several elements to be injected into your constructor. Something like that: `return [[Platform,Http,ElementRef]];` – Thierry Templier Mar 10 '16 at 18:02