# Custom component inside ion-header

**URL:** <https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757>\
**Category:** Ionic Framework\
**Created:** [March 11, 2019, 11:26am UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757 "2019-03-11T11:26:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Corbis](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/corbis/32/124922_2.png) [@Corbis](https://forum.ionicframework.com/u/Corbis)\
**Post date:** [March 11, 2019, 11:26am UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/1 "2019-03-11T11:26:52Z")

</div>

I created custom header component

```auto
...
@Component({
    selector: '[hd-header]',
    templateUrl: 'header.html',
    styleUrls: ['header.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeaderComponent implements OnInit {
    @Input() type = '';
    @Input() pageName = '';
    @Input() showSearch = false;
    @Input() showNotifications = false;
    @Input() showBag = false;

    @select(bagProductsCountSelector) productsCount: Observable<number>;
    @select(notficationsCountSelector) notificationCount: Observable<number>;
...

```

and I want to insert it in **ion-header** without host element.

In ionic v3 this worked  
`<ion-header hd-header [pageName]="'Sign up'"></ion-header>`

In ionic v4 it gives me error:  
**core.js:15723 ERROR Error: Uncaught (in promise): Error: Template parse errors:**  
**More than one component matched on this element.**  
**Make sure that only one component’s selector can match a given element.**  
**Conflicting components: IonHeader,HeaderComponent ("[ERROR -\>]**

Anyone can guide me to the right direction?

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [March 11, 2019, 12:06pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/2 "2019-03-11T12:06:34Z")

</div>

Yeah, that makes sense. You can’t instantiate two different components on the same DOM node.

What I usually do is to insert my custom component inside `ion-header` with just the contents that I want. It works perfectly.

Best,  
Rodrigo

---

<div class="post-metadata">

**Author:** ![Corbis](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/corbis/32/124922_2.png) [@Corbis](https://forum.ionicframework.com/u/Corbis)\
**Post date:** [March 11, 2019, 12:36pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/3 "2019-03-11T12:36:49Z")

</div>

Hey @FdezRomero do you insert custom component into `<ion-header></ion-header>` without host element?

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [March 11, 2019, 5:00pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/4 "2019-03-11T17:00:07Z")

</div>

No, I use my component inside like:

```auto
<ion-header>
  <app-my-header></app-my-header>
</ion-header>

```

Why do you need to include your component without a host element? Ionic needs the `ion-header` in order to correctly place it fixed at the top.

---

<div class="post-metadata">

**Author:** ![Corbis](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/corbis/32/124922_2.png) [@Corbis](https://forum.ionicframework.com/u/Corbis)\
**Post date:** [March 11, 2019, 5:50pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/5 "2019-03-11T17:50:43Z")

</div>

You understood me wrong let me explain, for example, I have this custom header element `hd-header`

```auto
@Component({
    selector: '[hd-header]',
    template: '
  <ion-toolbar>
    <ion-title>{{pageName}}</ion-title>
  </ion-toolbar>
',
    styleUrls: ['header.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeaderComponent implements OnInit {
    @Input() pageName = '';

```

And I to use this `hd-header` element without host element. I mean i want to render only whats inside `hd-header`.

This is not correct code, but maybe it will illustrate what I want to achieve.

```auto
<ion-header>
 <ng-content hd-header></ng-content>
</ion-header>

```

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [March 11, 2019, 6:03pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/6 "2019-03-11T18:03:53Z")

</div>

I see, thanks for the example. My question is why do you need to include the content directly without a parent or wrapping component. If there is a problem/need behind it or just because you like it better.

Have you tried using this component in an `ng-container` (which doesn’t render a parent node)? Like:

```auto
<ion-header>
 <ng-container hd-header></ng-container>
</ion-header>

```

I haven’t tried it myself but it might work…

---

<div class="post-metadata">

**Author:** ![Corbis](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/corbis/32/124922_2.png) [@Corbis](https://forum.ionicframework.com/u/Corbis)\
**Post date:** [March 11, 2019, 8:09pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/7 "2019-03-11T20:09:07Z")

</div>

Using this in ionic v4

```auto
<ion-header>
 <ng-container hd-header></ng-container>
</ion-header>

```

Gives me error

```auto
core.js:15723 ERROR Error: Uncaught (in promise): TypeError: el.setAttribute is not a function
TypeError: el.setAttribute is not a function

```

.  
.  
.

The reason is that at least In ionic v3(Haven’t tested yet ionic v4) I had problems with iphone x header styling when doing like this

```auto
<ion-header>
  <hd-header></hd-header>
</ion-header>

```

But overall If possible I would like to find a way to include the content directly without a parent or wrapping component, so I can keep html cleaner and html structure same as in ionic docs.

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [March 11, 2019, 8:16pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/8 "2019-03-11T20:16:16Z")

</div>

That error was kind of expected since the component doesn’t have a node rendered in DOM, so you can’t set attributes to it.

So maybe we should focus on solving the root cause: **What are your header styling problems in iPhone X?**

I have several Ionic apps in production and all look fine on my iPhone X, maybe I can help 🙂

---

<div class="post-metadata">

**Author:** ![Corbis](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/corbis/32/124922_2.png) [@Corbis](https://forum.ionicframework.com/u/Corbis)\
**Post date:** [March 11, 2019, 8:26pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/9 "2019-03-11T20:26:07Z")

</div>

I am still migrating my app to ionic v4, but if everything is ok on iPhone X, I guess I will just use

```auto
<ion-header>
  <hd-header</hd-header>
</ion-header>

```

---

<div class="post-metadata">

**Author:** ![FdezRomero](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fdezromero/32/110635_2.png) [@FdezRomero](https://forum.ionicframework.com/u/FdezRomero)\
**Post date:** [March 11, 2019, 8:37pm UTC](https://forum.ionicframework.com/t/custom-component-inside-ion-header/158757/10 "2019-03-11T20:37:42Z")

</div>

Got it 🙂 Let me know if you still have issues after you finished migrating to Ionic 4 👍
