Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Tuesday, January 10, 2017

105 Ionic Modal


.
105 CodePen Ionic Modal
The Modal is a content pane that can go over the user’s main view temporarily. Usually used for making a choice or editing an item.
CodePen Reference: http://codepen.io/ionic/pen/gblny 

1) Create Modal as HTML Script

HTML
<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
   
    <title>Ionic Modal</title>
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  </head>
  <body ng-controller="AppCtrl">
   
    <ion-header-bar class="bar-positive">
      <h1 class="title">Contacts</h1>
      <div class="buttons">
        <button class="button button-icon ion-compose" ng-click="modal.show()">
        </button>
      </div>
    </ion-header-bar>
    <ion-content>
      <ion-list>
        <ion-item ng-repeat="contact in contacts">
          {{contact.name}}
        </ion-item>
      </ion-list>
    </ion-content>
   
    <script id="templates/modal.html" type="text/ng-template">
      <ion-modal-view>
        <ion-header-bar class="bar bar-header bar-positive">
          <h1 class="title">New Contact</h1>
          <button class="button button-clear button-primary" ng-click="modal.hide()">Cancel</button>
        </ion-header-bar>
        <ion-content class="padding">
          <div class="list">
            <label class="item item-input">
              <span class="input-label">First Name</span>
              <input ng-model="newUser.firstName" type="text">
            </label>
            <label class="item item-input">
              <span class="input-label">Last Name</span>
              <input ng-model="newUser.lastName" type="text">
            </label>
            <label class="item item-input">
              <span class="input-label">Email</span>
              <input ng-model="newUser.email" type="text">
            </label>
            <button class="button button-full button-positive" ng-click="createContact(newUser)">Create</button>
          </div>
        </ion-content>
      </ion-modal-view>
    </script>
   
  </body>
</html>
JS
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();
 };

});
See the Pen 105 CodePen Ionic Modal 1 by notarazi (@notarazi) on CodePen.

2) Create Modal as Embedded HTML in JS

HTML
<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
   
    <title>Ionic Modal</title>
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  </head>
  <body ng-controller="AppCtrl">
   
    <ion-header-bar class="bar-positive">
      <h1 class="title">Contacts</h1>
      <div class="buttons">
        <button class="button button-icon ion-compose" ng-click="modal.show()">
        </button>
      </div>
    </ion-header-bar>
    <ion-content>
      <ion-list>
        <ion-item ng-repeat="contact in contacts">
          {{contact.name}}
        </ion-item>
      </ion-list>
    </ion-content>
   
    <script id="templates/modal.html" type="text/ng-template">    
    </script>
   
  </body>
</html>
Cut the html codes contained within the highlighted script tag above.
Optionally, minify the scripts. You can use any of these; http://minifycode.com/html-minifier/ , https://kangax.github.io/html-minifier/ or http://www.willpeavy.com/minifier/ .
JS
angular.module('ionicApp', ['ionic'])
.controller('AppCtrl', function($scope, $ionicModal) {

  $scope.contacts = [
    { name: 'Gordon Freeman' },
    { name: 'Barney Calhoun' },
    { name: 'Lamarr the Headcrab' },
  ];
 $scope.modal = $ionicModal.fromTemplate(' <ion-modal-view> <ion-header-bar class="bar bar-header bar-positive"> <h1 class="title">New Contact</h1> <button class="button button-clear button-primary" ng-click="modal.hide()">Cancel</button> </ion-header-bar> <ion-content class="padding"> <div class="list"> <label class="item item-input"> <span class="input-label">First Name</span> <input ng-model="newUser.firstName" type="text"> </label> <label class="item item-input"> <span class="input-label">Last Name</span> <input ng-model="newUser.lastName" type="text"> </label> <label class="item item-input"> <span class="input-label">Email</span> <input ng-model="newUser.email" type="text"> </label> <button class="button button-full button-positive" ng-click="createContact(newUser)">Create</button> </div></ion-content> </ion-modal-view>', {
    scope: $scope
  });

  $scope.createContact = function(u) {        
    $scope.contacts.push({ name: u.firstName + ' ' + u.lastName });
    $scope.modal.hide();
  };
});
See the Pen 105 CodePen Ionic Modal 2 by notarazi (@notarazi) on CodePen.

3) Adding Open and Close Function

Besides Open and Close, we also put three additional event-related functions; destroy, hidden and removed.
With Open and Close functions, we would be able to do additional task during the opening and closing of the modal.
HTML
<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
   
    <title>Ionic Modal</title>
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  </head>
  <body ng-controller="AppCtrl">
   
    <ion-header-bar class="bar-positive">
      <h1 class="title">Contacts</h1>
      <div class="buttons">
        <button class="button button-icon ion-compose" ng-click="openModal()">
        </button>
      </div>
    </ion-header-bar>
    <ion-content>
      <ion-list>
        <ion-item ng-repeat="contact in contacts">
          {{contact.name}}
        </ion-item>
      </ion-list>
    </ion-content>
   
   
  </body>
</html>
angular.module('ionicApp', ['ionic'])
.controller('AppCtrl', function($scope, $ionicModal) {

  $scope.contacts = [
    { name: 'Gordon Freeman' },
    { name: 'Barney Calhoun' },
    { name: 'Lamarr the Headcrab' },
  ];
 $scope.modal = $ionicModal.fromTemplate(' <ion-modal-view> <ion-header-bar class="bar bar-header bar-positive"> <h1 class="title">New Contact</h1> <button class="button button-clear button-primary" ng-click="closeModal()">Cancel</button> </ion-header-bar> <ion-content class="padding"> <div class="list"> <label class="item item-input"> <span class="input-label">First Name</span> <input ng-model="newUser.firstName" type="text"> </label> <label class="item item-input"> <span class="input-label">Last Name</span> <input ng-model="newUser.lastName" type="text"> </label> <label class="item item-input"> <span class="input-label">Email</span> <input ng-model="newUser.email" type="text"> </label> <button class="button button-full button-positive" ng-click="createContact(newUser)">Create</button> </div></ion-content> </ion-modal-view>', {
    scope: $scope
  });

   $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
   }); 

  $scope.createContact = function(u) {        
    $scope.contacts.push({ name: u.firstName + ' ' + u.lastName });
    $scope.modal.hide();
  };
});

See the Pen 105 CodePen Ionic Modal 3 by notarazi (@notarazi) on CodePen.

.

No comments:

Post a Comment