ionic Modal
$ionicModal
$ionicModal can cover the content box of the user's main interface.
You can embed the following code in your index file or another file (the code inside can be changed according to your own business scenario).
Then you can inject $ionicModal in your Controller, then call the template you just wrote to initialize, like the following code:
angular.module('testApp', ['ionic'])
.controller('MyController', function($scope, $ionicModal) {
$ionicModal.fromTemplateUrl('my-modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
//Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Execute action on hide modal
$scope.$on('modal.hidden', function() {
// Execute action
});
// Execute action on remove modal
$scope.$on('modal.removed', function() {
// Execute action
});
});
Method
fromTemplate(templateString, options)
| Parameters | Type | Details |
|---|---|---|
| templateString | 字符串 |
The template string serves as the content of the modal. |
| options | 对象 |
options will be passed to the ionicModal#initialize method. |
Returns: object, an instance of an ionicModal controller.
fromTemplateUrl(templateUrl, options)
| Parameters | Type | Details |
|---|---|---|
| templateUrl | 字符串 |
The url of the template to load. |
| options | 对象 |
The object passed through the ionicModal#initialize method. |
Returns: promise object. The Promises object is a specification proposed by the CommonJS working group to provide a unified interface for asynchronous programming.
ionicModal
Instantiated by the $ionicModal service.
Tip: when you finish cleaning up each module, make sure to call the remove() method to avoid memory leaks.
Note: a module broadcasts 'modal.shown' and 'modal.hidden' from its initial scope, passing itself as an argument.
Method
initialize(可选)
An example of creating a new modal controller.
| Parameters | Type | Details |
|---|---|---|
| options | 对象 |
An options object has the following properties: |
{object=} 范围 The scope of the child. Default: create a $rootScope child.{string=} 动画 Animation for showing or hiding. Default: 'slide-in-up'{boolean=} 第一个输入框获取焦点 Whether the first input element of the modal automatically gets focus when shown. Default: false.{boolean=}backdropClickToClose` whether to close the modal when clicking the backdrop. Default: true.show()
Show the modal instance
- Return value:
promisepromise object, resolved after the modal completes its animation
hide()
Hide the modal.
- Return value:
promisepromise object, resolved after the modal completes its animation
remove()
Remove the modal instance from the DOM and clean up.
- Return value:
promisepromise object, resolved after the modal completes its animation
isShown()
- Returns: boolean, used to determine whether the modal is shown.
Example
HTML Code
Runoob Tutorial(runoob.com)
Contacts
{{contact.name}}
CSS code
body {
cursor: url('http://www.runoob.com/try/demo_source/finger.png'), auto;
}
JavaScript Code
angular.module('ionicApp', ['ionic'])
.controller('AppCtrl', function($scope, $ionicModal) {
$scope.contacts = [
{ name: 'Gordon Freeman' },
{ name: 'Barney Calhoun' },
{ name: 'Lamarr the Headcrab' },
];
$ionicModal.fromTemplateUrl('templates/modal.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
$scope.createContact = function(u) {
$scope.contacts.push({ name: u.firstName + ' ' + u.lastName });
$scope.modal.hide();
};
});