Jumat, 11 Agustus 2017

Ionic - JavaScript Popup

This service is used for creating a popup window on top of the regular view, which will be used for interaction with the users. There are four types of popups namely − showconfirmalert and prompt.

Using Show Popup

This popup is the most complex of all. To trigger popups, we need to inject the $ionicPopup service to our controller and then just add a method that will trigger the popup we want to use, in this case $ionicPopup.show(). The onTap(e) function can be used for adding e.preventDefault() method, which will keep the popup open, if there is no change applied to the input. When the popup is closed, the promised object will be resolved.

Controller Code

.controller('MyCtrl', function($scope, $ionicPopup) {

   // When button is clicked, the popup will be shown...
   $scope.showPopup = function() {
      $scope.data = {}
    
      // Custom popup
      var myPopup = $ionicPopup.show({
         template: '<input type = "text" ng-model = "data.model">',
         title: 'Title',
         subTitle: 'Subtitle',
         scope: $scope,
   
         buttons: [
            { text: 'Cancel' }, {
               text: '<b>Save</b>',
               type: 'button-positive',
               onTap: function(e) {
      
                  if (!$scope.data.model) {
                     //don't allow the user to close unless he enters model...
                     e.preventDefault();
                  } else {
                     return $scope.data.model;
                  }
               }
            }
         ]
      });

      myPopup.then(function(res) {
         console.log('Tapped!', res);
      });    
   };

})

HTML Code

<button class = "button" ng-click = "showPopup()">Add Popup Show</button>
Ionic Popup Show
You probably noticed in the above-mentioned example some new options were used. The following table will explain all of those options and their use case.

Show Popup Options

OptionTypeDetails
templatestringInline HTML template of the popup.
templateUrlstringURL of the HTML template.
titlestringThe title of the popup.
subTitlestringThe subtitle of the popup.
cssClassstringThe CSS class name of the popup.
scopeScopeA scope of the popup.
buttonsArray[Object]Buttons that will be placed in footer of the popup. They can use their own properties and methods. text is displayed on top of the button, type is the Ionic class used for the button, onTap is function that will be triggered when the button is tapped. Returning a value will cause the promise to resolve with the given value.

Using Confirm Popup

A Confirm Popup is the simpler version of Ionic popup. It contains Cancel and OK buttons that users can press to trigger the corresponding functionality. It returns the promised object that is resolved when one of the buttons are pressed.

Controller Code

.controller('MyCtrl', function($scope, $ionicPopup) {

   // When button is clicked, the popup will be shown...
   $scope.showConfirm = function() {
 
      var confirmPopup = $ionicPopup.confirm({
         title: 'Title',
         template: 'Are you sure?'
      });

      confirmPopup.then(function(res) {
         if(res) {
            console.log('Sure!');
         } else {
            console.log('Not sure!');
         }
      });
  
   };

})

HTML Code

<button class = "button" ng-click = "showConfirm()">Add Popup Confirm</button>
Ionic Popup Confirm
The following table explains the options that can be used for this popup.

Confirm Popup Options

OptionTypeDetails
templatestringInline HTML template of the popup.
templateUrlstringURL of the HTML template.
titlestringThe title of the popup.
subTitlestringThe subtitle of the popup.
cssClassstringThe CSS class name of the popup.
cancelTextstringThe text for the Cancel button.
cancelTypestringThe Ionic button type of the Cancel button.
okTextstringThe text for the OK button.
okTypestringThe Ionic button type of the OK button.

Using Alert Popup

An Alert is a simple popup that is used for displaying the alert information to the user. It has only one button that is used to close the popup and resolve the popups’ promised object.

Controller Code

.controller('MyCtrl', function($scope, $ionicPopup) {

   $scope.showAlert = function() {
 
      var alertPopup = $ionicPopup.alert({
         title: 'Title',
         template: 'Alert message'
      });

      alertPopup.then(function(res) {
         // Custom functionality....
      });
   };
})

HTML Code

<button class = "button" ng-click = "showAlert()">Add Popup Alert</button>
It will produce the following screen −
Ionic Popup Alert
The following table shows the options that can be used for an alert popup.

Alert Popup Options

OptionTypeDetails
templatestringInline HTML template of the popup.
templateUrlstringURL of the HTML template.
titlestringThe title of the popup.
subTitlestringThe subtitle of the popup.
cssClassstringThe CSS class name of the popup.
okTextstringThe text for the OK button.
okTypestringThe Ionic button type of the OK button.

Using Prompt Popup

The last Ionic popup that can be created using Ionic is prompt. It has an OK button that resolves promise with value from the input and Cancel button that resolves with undefined value.

Controller Code

.controller('MyCtrl', function($scope, $ionicPopup) {

   $scope.showPrompt = function() {
 
      var promptPopup = $ionicPopup.prompt({
         title: 'Title',
         template: 'Template text',
         inputType: 'text',
         inputPlaceholder: 'Placeholder'
      });
        
      promptPopup.then(function(res) {
         console.log(res);
      });
  
   };
})

HTML Code

<button class = "button" ng-click = "showPrompt()">Add Popup Prompt</button>
It will produce the following screen −
Ionic Popup Prompt
The following table shows options that can be used for a prompt popup.

Prompt Popup Options

OptionTypeDetails
templatestringInline HTML template of the popup.
templateUrlstringURL of the HTML template.
titlestringThe title of the popup.
subTitlestringThe subtitle of the popup.
cssClassstringThe CSS class name of the popup.
inputTypestringThe type for the input.
inputPlaceholderstringA placeholder for the input.
cancelTextstringThe text for the Cancel button.
cancelTypestringThe Ionic button type of the Cancel button.
okTextstringThe text for the OK button.
okTypestringThe Ionic button type of the OK button.

Tidak ada komentar:

Posting Komentar