ionic tutorial - Ionic Modal - ionic - ionic development - ionic 2 - ionic framework
- The Ionic JavaScript Modal is a larger popup with more functions and when activated enables the content pane to appear on the top of regular content.
- By default, Modal will cover the entire screen but can be optimized as desired.
- When Ionic modal is activated, the content pane will appear on top of the regular content.
- Modal is basically larger popup with more functionalities.
Using Modal
- There are two ways of implementing modal in Ionic. One way is to add separate template and the other is to add it on top of the regular HTML file, inside script tags.
- First thing we need to do is to connect our modal to our controller using angular dependency injection.
- Then we need to create modal. We will pass in scope and add animation to our modal.
- After that we are creating functions for opening, closing, destroying modal and the last two functions are place where we can write code that will be triggered if modal is hidden or removed.
- If you don't want to trigger any functionality when modal is removed or hidden you can delete the last two functions.
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
Controller Code
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
HTML Code
- The way we showed in last example is when script tag is used as a container to our modal inside some existing HTML file.
- The second way is to create new template file inside templates folder.
- We will use the same code as in our last example but we will remove script tags and we also need to change fromTemplateUrl in controller to connect modal with new created template.
Controller Code
HTML Code
- Third way of using Ionic modal is by inserting HTML inline. We will use fromTemplate instead of fromTemplateUrl.
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
Controller Code
- All tree examples will have the same effect. We will create button to trigger $ionicModal.show() to open modal.
ionic tutorial . extension , ionic framework , ionic , ionic framework book , ionic app example , ionic templates , ionic getting started
HTML Code
- When we open modal, it will contain button that will be used for closing it. We created this button in HTML template.
- There are also other options for modal optimization. We already showed how to use scope and animation. The table below shows other options.
Option | Type | Detail |
---|---|---|
focusFirstInput | boolean | It will auto focus first input of the modal. |
backdropClickToClose | boolean | It will enable closing the modal when backdrop is tapped. Default value is true. |
hardwareBackButtonClose | boolean | It will enable closing the modal when hardware back button is clicked. Default value is true. |