Tushar Balar
Tushar Balar

Reputation: 751

In Ionic 2, how do I create a custom component 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.

@Component({
    selector: 'nl-navbar',
    template: `
        <ion-header>
          <ion-navbar color="primary">
            <button ion-button menuToggle>
              <ion-icon name="menu"></ion-icon>
            </button>
            <ion-title >
              <span>Complaints</span>
            </ion-title>
            <ion-buttons *ngIf="!hideCreateButton" end>
              <button ion-button class="navBtnRight" (click)="openPopover($event)">
                <ion-icon name="md-more"></ion-icon>
              </button>
            </ion-buttons>
          </ion-navbar>
        </ion-header>
    `,
    inputs: ['hideCreateButton']
})

export class CustomNavbar {

    public hideCreateButton: string;

    constructor(public popoverCtrl: PopoverController) {
    }

    createNew(): void {
        // this.nav.setRoot(CreateNewPage, {}, { animate: true, direction: 'forward' });
    }

    openPopover(myEvent) {
      let popover = this.popoverCtrl.create(PopoverPage);
      popover.present({
        ev: myEvent
      });
    }
}

using custom navbar like this:

<nl-navbar [hideCreateButton]="hidebutton()"></nl-navbar>

and my ts file looks like this:

private hideCreateButton: boolean = false;

  public hidebutton(): boolean {
    return this.hideCreateButton;
  }

The directive is rendered, but Ionic components are not transformed, and so wont look/work properly. Sometimes it works in android device only.

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

Upvotes: 2

Views: 1915

Answers (1)

reflog
reflog

Reputation: 7645

Make sure that the module that contains your custom component has: imports: [IonicModule]

Upvotes: 2

Related Questions