# Modal via including template?

**URL:** <https://forum.ionicframework.com/t/modal-via-including-template/1360>\
**Category:** ionic-v1\
**Created:** [February 25, 2014, 7:07pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360 "2014-02-25T19:07:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![guyz](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@guyz](https://forum.ionicframework.com/u/guyz)\
**Post date:** [February 25, 2014, 7:07pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/1 "2014-02-25T19:07:03Z")

</div>

In the modal example it mentions that instead of using inline, you can include a template.

My current “bad” code for a modal is very WET if i want the same modal available throughout my app

```
<script id="seemingly-arbitrary-name.html" type=text/ng-template">
your html content here copy/pasted for each view you want the modal on currently
</script>

```

I’d prefer to include it via my-modal-template.html so that i can just call it on any view without having to repeat the code; but i can’t figure it out and the example failed to provide an example.

---

<div class="post-metadata">

**Author:** ![dave](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dave/32/64295_2.png) [@dave](https://forum.ionicframework.com/u/dave)\
**Post date:** [February 25, 2014, 7:21pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/2 "2014-02-25T19:21:29Z")

</div>

You can specify a template URL like so:

```auto
  $ionicModal.fromTemplateUrl('views/modals/new-exercise.html', function(modal) {
    $scope.newExerciseModal = modal;
  }, {
    scope: $scope,
    animation: 'slide-in-up'
  });

```

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [February 25, 2014, 7:22pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/3 "2014-02-25T19:22:59Z")

</div>

@dave beat me to it.

Here’s an example :  
https://codepen.io/calendee/embed/preview/oCLJF?height=300&slug-hash=oCLJF&default-tabs=js,result&host=https://codepen.io

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [February 25, 2014, 7:24pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/4 "2014-02-25T19:24:05Z")

</div>

Here’s another :

> [@Any modal examples/demos?](http://forum.ionicframework.com/t/any-modal-examples-demos/1350):
>
> I’m looking for an example of how to use a Modal. Can’t seem to figure it out for whatever reason. The example in the docs isn’t very descriptive. Just trying to do a simple “Forgot Password” modal.

---

<div class="post-metadata">

**Author:** ![guyz](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@guyz](https://forum.ionicframework.com/u/guyz)\
**Post date:** [February 25, 2014, 8:50pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/5 "2014-02-25T20:50:07Z")

</div>

So the code examples look identical to what I have (except for $scope.newExerciseModal) which doesn’t seem to have any change.

As soon as I remove the `<script id="modal.html" type="text/ng-template">` from the view, it doesn’t work. I have a separate page called “modal.html” that contains what would have been inside the `<script>` tags

```
.controller('modal', function($scope, $ionicModal) {
  // Load the modal from the given template URL
  $ionicModal.fromTemplateUrl('modal.html', function(modal) { $scope.modal = modal; }, 
  {
    // Use our scope for the scope of the modal to keep it simple
    scope: $scope,
    // The animation we want to use for the modal entrance
    animation: 'slide-in-up'
  });
  $scope.openModal = function() {
    $scope.modal.show();
  };
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
})

```

My NON-working view (trying to use external template modal.html):

```
<view>
<div data-ng-controller="modal" data-ng-click="openModal()">click</div>
</view>

```

Working View:

```
<script id="modal.html" type="text/ng-template">
modal content here
</script>
<view>
<div data-ng-controller="modal" data-ng-click="openModal()">click</div>
</view>

```

Where the working view will require me to copy/paste the code for each view I want to have the modal (if i needed to edit a spelling error, id have to edit multiple instances)

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [February 25, 2014, 9:07pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/6 "2014-02-25T21:07:37Z")

</div>

As best I can understand, you have a view file with the modal template inside it.

If so, you need to put the `<script id="modal.html" type="text/ng-template">` template in it’s own file. You can put in your index.html file. Then, any controller that needs that modal can reference it.

If that doesn’t do the trick, can you make a Codepen sample so we can see what’s wrong?

---

<div class="post-metadata">

**Author:** ![rpthis2](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rpthis2](https://forum.ionicframework.com/u/rpthis2)\
**Post date:** [February 26, 2014, 4:25pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/8 "2014-02-26T16:25:23Z")

</div>

inside my index.html I would like to store my templates elsewhere.  
I’m currently getting an error when I load my template outside

---

<div class="post-metadata">

**Author:** ![rpthis2](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rpthis2](https://forum.ionicframework.com/u/rpthis2)\
**Post date:** [February 26, 2014, 4:27pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/9 "2014-02-26T16:27:11Z")

</div>

I need load my $ionicModal.fromTemplateUrl(‘views/modals/new-exercise.html’) outside the index.html file but I get an error. Any examples of this thx.

---

<div class="post-metadata">

**Author:** ![jog00](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@jog00](https://forum.ionicframework.com/u/jog00)\
**Post date:** [February 28, 2014, 6:05am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/10 "2014-02-28T06:05:45Z")

</div>

To load a modal dialog from a file using:

```
$ionicModal.fromTemplateUrl('templates/new-modal.html', function(modal) {
  $scope.modal = modal;
}, {
  scope: $scope,
  animation: 'slide-in-up'
});

```

you must ensure that the very first line of the file is a div element, i.e.

```
<div class="modal">

```

If the first line is a comment the modal will not load. This applies to ionic 0.9.26.

---

<div class="post-metadata">

**Author:** ![praveen](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@praveen](https://forum.ionicframework.com/u/praveen)\
**Post date:** [July 2, 2014, 4:50am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/11 "2014-07-02T04:50:11Z")

</div>

@guyz Have you found the opt solution? Please let me know your approach.

Thanks

---

<div class="post-metadata">

**Author:** ![praveen](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@praveen](https://forum.ionicframework.com/u/praveen)\
**Post date:** [July 2, 2014, 5:09am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/12 "2014-07-02T05:09:57Z")

</div>

@Calendee I too have a similar question [**here**](http://forum.ionicframework.com/t/how-to-load-modal-window-from-templates/6270). As per your example I have added `my-modal.html script` within `Index.html` and it is working fine [**#fiddle1**](http://plnkr.co/edit/drLhIy7GWfV51KfsNb8l?p=preview) 😄

Now I am trying to keep it within an template `my-modal.html` like I mentioned in my question and here the `my-modal.html` not working [**#fiddle2**](http://plnkr.co/edit/drLhIy7GWfV51KfsNb8l?p=preview) ☹

As per the error in console, I can understand that I missing to point the `$scope` and therefore it is not working. But I am unable to fix it up. Please let me know your suggestions.

Thanks,  
Praveen

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [July 2, 2014, 9:58am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/13 "2014-07-02T09:58:05Z")

</div>

The problem simply seems to be with timing. I changed the timeout on the second example to 100ms and it worked fine. I imagine it has to do with the quality of the connection to the Plunker servers. I don’t know why the first loads so well. I can’t see any difference in the code at all.

Keep in mind that on a device, the entire HTML will be packaged and served locally; so, I don’t think you will have this problem.

> **[Plunker](http://plnkr.co/edit/I4Jj9D5MigopDuySm2Bu)**
>
> Created on Plnkr: Helping developers build the web.

---

<div class="post-metadata">

**Author:** ![duall](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@duall](https://forum.ionicframework.com/u/duall)\
**Post date:** [July 16, 2014, 1:54am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/14 "2014-07-16T01:54:36Z")

</div>

i was having also problems, tried with javascript line and other stuff with no avail, mine ended working like this:

in controller:

```
$ionicModal.fromTemplateUrl('/nivi/www/templates/partials/produinfodialog.html', {
      scope: $scope, 
      animation: 'slide-in-up'
 }).then(function(modal) { $scope.modal = modal; modal.show();});

```

in view:

```
<div class="modal">
  <ion-header-bar class="bar bar-header bar-positive">
    <h1 class="title">Informacion del Producto</h1>
    <div class="button button-clear" ng-click="modal.hide()"><span class="icon ion-close"></span></div>
  </ion-header-bar>
  <ion-content has-header="true" has-subheader="false" scroll="true">
    <p>This is a modal</p>
        <ul class="list" id="color_to_pick_list">
            <li ng-repeat="combination in combinations" class="item">
                        <a title="{{combination.color_name}}" style="background:{{combination.color_hex}}" class="color_pick selected" 
name="{{combination.color_name}}" id="color_7" href="#">aca</a>
                </li>
        </ul>
     <input type="hidden" value="7" name="group_3" class="color_pick_hidden">
  </ion-content>
</div>

```

---

<div class="post-metadata">

**Author:** ![naveen007](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@naveen007](https://forum.ionicframework.com/u/naveen007)\
**Post date:** [September 19, 2014, 9:43pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/15 "2014-09-19T21:43:32Z")

</div>

Ok…I also encountered this issue. This comes because we are just including the template to a DIV which is supposed to be the only way and DIV are by default shown. So just hide it. OR use ng-hide class.

```
<div ng-include="'templates/new-task.html' " class="ng-hide"></div>
```

---

<div class="post-metadata">

**Author:** ![naveen007](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@naveen007](https://forum.ionicframework.com/u/naveen007)\
**Post date:** [September 19, 2014, 9:44pm UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/16 "2014-09-19T21:44:09Z")

</div>

use this. It will hide the div.

---

<div class="post-metadata">

**Author:** ![apunn](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@apunn](https://forum.ionicframework.com/u/apunn)\
**Post date:** [April 29, 2015, 7:16am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/17 "2015-04-29T07:16:39Z")

</div>

Has there been any update to this? I am having the same problem as @praveen. Loading a modal from an external template is not working at all. It is doable if I include the modal code inside a script tag inside the .html file. Also FYI, I am doing this in an integrated meteor-ionic-angular app, but I don’t think that should change anything as every other ionic component is working correctly.

---

<div class="post-metadata">

**Author:** ![apunn](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@apunn](https://forum.ionicframework.com/u/apunn)\
**Post date:** [May 5, 2015, 5:09am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/18 "2015-05-05T05:09:47Z")

</div>

@Calendee Can you please comment on this?

---

<div class="post-metadata">

**Author:** ![apunn](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@apunn](https://forum.ionicframework.com/u/apunn)\
**Post date:** [May 17, 2015, 1:14am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/19 "2015-05-17T01:14:01Z")

</div>

Can I please get some help on this? I posted about this 18 days ago and still haven’t received a single reply. @Calendee can you please advice?

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [May 17, 2015, 1:40am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/20 "2015-05-17T01:40:33Z")

</div>

Modals from template URLs (`$ionicModal.fromTemplateUrl`) (rather than inline) work pretty well across the board. So, I don’t know exactly what problem you’re having. I frequently have a completely separate “myModal.html” file in my app - rather than inline scripts.

Do you have a Codepen or Ionic Playground sample that demonstrates the problem? Or can you post the modal code here and the call to `$ionicModal`?

---

<div class="post-metadata">

**Author:** ![apunn](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@apunn](https://forum.ionicframework.com/u/apunn)\
**Post date:** [June 1, 2015, 3:38am UTC](https://forum.ionicframework.com/t/modal-via-including-template/1360/21 "2015-06-01T03:38:10Z")

</div>

I realized what the problem was because my code was organized in a file structure with meteor. I had to create a separate /modals directory to save my custom modals. I describe my problem and solution here: [https://github.com/Urigo/meteor-angular-socially/issues/28](https://github.com/Urigo/meteor-angular-socially/issues/28)
