# Issue with template in ion-item

**URL:** <https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336>\
**Category:** ionic-v3\
**Created:** [September 5, 2017, 4:23pm UTC](https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336 "2017-09-05T16:23:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThibaultLap](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@ThibaultLap](https://forum.ionicframework.com/u/ThibaultLap)\
**Post date:** [September 5, 2017, 4:23pm UTC](https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336/1 "2017-09-05T16:23:52Z")

</div>

Hello,

I’m working on an ionic 2 application since many months and since some times, I had an issue that appeared and I don’t find a solution.  
Here the code:

```auto
<template ngFor let-actionType_l [ngForOf]="actions_m">
    <ion-item no-lines>
        <ion-label>
            {{actionType_l.actionType}}
        </ion-label>
        <ion-checkbox [(ngModel)]="actionType_l.checked"></ion-checkbox>
    </ion-item>
    <ion-item
        no-lines>
        <template id="templateAction" *ngIf="actionType_l.checked" ngFor let-action_l [ngForOf]="actionType_l.actions">
            <ion-item
                no-lines>
                <ion-label>
                    {{action_l.actionNameToShow}}
                </ion-label>
                <ion-checkbox [(ngModel)]="action_l.checked"></ion-checkbox>
            </ion-item>
        </template>
    </ion-item>
</template>

```

It works fine for a while but now the template with id “templateAction” never appear. If I remove the ion-item around the template all the data appear but it doesn’t have the good layout.

For information, I use ionic framework version 2.2.0 and ionic cli version 2.2.3

Does anyone have a solution to resolve my issue?

Thanks by advance,  
Thibault

---

<div class="post-metadata">

**Author:** ![luukschoen](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/luukschoen/32/98585_2.png) [@luukschoen](https://forum.ionicframework.com/u/luukschoen)\
**Post date:** [September 5, 2017, 6:00pm UTC](https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336/2 "2017-09-05T18:00:20Z")

</div>

```auto
<template id="templateAction" *ngIf="actionType_l.checked" ngFor let-action_l [ngForOf]="actionType_l.actions">
            <ion-item
                no-lines>
                <ion-label>
                    {{action_l.actionNameToShow}}
                </ion-label>
                <ion-checkbox [(ngModel)]="action_l.checked"></ion-checkbox>
            </ion-item>
        </template>

```

I don’t know how that could have worked at all, since it uses two directives at the same level (ngIf and ngFor). Put the ngIf on the ion-item if you need it, loop over the template with the ngFor like your already doing. What’s that supposed to do btw?

---

<div class="post-metadata">

**Author:** ![lucasbasquerotto](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/lucasbasquerotto/32/101719_2.png) [@lucasbasquerotto](https://forum.ionicframework.com/u/lucasbasquerotto)\
**Post date:** [September 5, 2017, 6:32pm UTC](https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336/3 "2017-09-05T18:32:29Z")

</div>

@ThibaultLap In a `template` you should `[ngIf]` instead of `*ngIf` and you **shouldn’t use more than 1 structural directive** (in your case, `ngIf` and `ngFor`) in the same component.

**Also, instead of `template` I would recommend using `ng-container`** to use the same syntax as the other components. Its just a syntatic sugar, but very useful.

There is also the fact that the `template` and `ng-container` **don’t appear in the HTML**. It’s a good thing, to avoid a lot of nested elements inside others unnecessarily, problems with css selectors, among other problems.

_So instead of:_

```auto
<template id="templateAction" *ngIf="actionType_l.checked" ngFor let-action_l [ngForOf]="actionType_l.actions">
	...
</template>

```

_You can do:_

```auto
<ng-container *ngIf="actionType_l.checked">
	<ng-container *ngFor="let action_l of actionType_l.actions">
		...
	</ng-container>
</ng-container>

```

If you really want to have the `id` be shown in an element, use it in a element like `div` or similar, but I don’t see a point in doing that.

You can see more about `ng-container` [here](https://stackoverflow.com/a/40530244/4850646).

And I really recommend that you read the Angular docs about structural directives: [https://angular.io/guide/structural-directives](https://angular.io/guide/structural-directives).

---

<div class="post-metadata">

**Author:** ![ThibaultLap](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@ThibaultLap](https://forum.ionicframework.com/u/ThibaultLap)\
**Post date:** [September 6, 2017, 8:18am UTC](https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336/4 "2017-09-06T08:18:43Z")

</div>

@luukschoen I already tried to put the ngIf on the ion-item and it didn’t work.

@lucasbasquerotto Thanks for your advice but even with that, it doesn’t work. By the way I have the same problem in another file where I have that:

```auto
<template ngFor let-action_l [ngForOf]="actions_m">
    <ion-item
        no-lines>
        <ion-label>
            {{action_l.name}}
        </ion-label>
        <ion-checkbox [(ngModel)]="action_l.checked"></ion-checkbox>
    </ion-item>
    <ion-item *ngIf="action_l.id === 'power' && action_l.checked"
        no-lines>
         <!--<ion-select [(ngModel)]="action_l.boxelDeviceCommandArgs[0].value">-->
         <!-- <ion-option value="toggle">Toggle</ion-option>-->
         <!-- <ion-option value="on">On</ion-option>-->
         <!-- <ion-option value="off">Off</ion-option>-->
         <!--</ion-select>-->
         <ion-list radio-group [(ngModel)]="action_l.value">
             <ion-item>
                 <ion-label>Toggle</ion-label>
                 <ion-radio value="toggle"></ion-radio>
             </ion-item>
             <ion-item>
                 <ion-label>On</ion-label>
                 <ion-radio value="on"></ion-radio>
             </ion-item>
             <ion-item>
                 <ion-label>Off</ion-label>
                 <ion-radio value="off"></ion-radio>
             </ion-item>
         </ion-list>
     </ion-item>
     <ion-item *ngIf="action_l.id === 'color' && action_l.checked"
         no-lines>
         <ion-range [(ngModel)]="action_l.value1000" min="0" max="1000" step="1"></ion-range>
     </ion-item>
</template>

```

In that case, the range in the item with “action\_l.id === ‘color’” appear but the list in the item with “action\_l.id === ‘power’” never appear even if the select appear if I uncomment it but it has not the good layout. It seems like that I cant have an ion-item into another anymore whereas before that worked.

---

<div class="post-metadata">

**Author:** ![lucasbasquerotto](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/lucasbasquerotto/32/101719_2.png) [@lucasbasquerotto](https://forum.ionicframework.com/u/lucasbasquerotto)\
**Post date:** [September 6, 2017, 11:40am UTC](https://forum.ionicframework.com/t/issue-with-template-in-ion-item/104336/5 "2017-09-06T11:40:38Z")

</div>

@ThibaultLap The behaviour of `ion-item` is a bit specific and it doesn’t allow all kinds of components inside it, although you can make the `ion-item` _see it_ using the `item-content` directive:

```auto
<ion-list radio-group [(ngModel)]="action_l.value" item-content>
    ...
</ion-list>

```

See more about `ion-item` [here](https://ionicframework.com/docs/api/components/item/Item/).

**Or** you can just use `ng-container` or similar instead of the external `ion-item` (nested `ion-items` may cause unexpected results, and you already have `ion-item` inside your `ion-list`):

```auto
<ng-container *ngIf="action_l.id === 'power' && action_l.checked" no-lines>

```
