Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ionic 2, how do I create a custom directive that uses Ionic components?

Creating a basic directive is simple:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-component',
    template: '<div>Hello!</div>'
})
export class MyComponent {
    constructor() {

    }
}

This works as expected. However, if I want to use Ionic components in my directive things blow up.

import {Component} from 'angular2/core';

@Component({
    selector: 'my-component',
    template: '<ion-list><ion-item>I am an item</ion-item></ion-list>'
})
export class MyComponent {
    constructor() {

    }
}

The directive is rendered, but Ionic components are not transformed, and so wont look/work properly.

I can't find any examples on this. How should I do this?

like image 702
Schlaus Avatar asked Jan 24 '16 13:01

Schlaus


Video Answer


1 Answers

Found the answer here:

You have to import the Ionic components and register them as 'directives'

So my second example becomes:

import {Component} from 'angular2/core';
import {List, Item} from 'ionic/ionic';

@Component({
    selector: 'my-component',
    directives: [List, Item],
    template: '<ion-list><ion-item>I am an item</ion-item></ion-list>'
})
export class MyComponent {
    constructor() {

    }
}
like image 154
Schlaus Avatar answered Sep 22 '22 17:09

Schlaus