Ionic – Cordova Media ”; Previous Next This plugin allows us to record and playback audio files on a device. Using Media As with all the other Cordova plugins, the first thing we need to do is to install it from the command prompt window. C:UsersUsernameDesktopMyApp>cordova plugin add cordova-plugin-media Now, we are ready to use the plugin. In the following code sample, src is the source mp3 file that we will use for this tutorial. It is placed in js folder, but we need to add /android_asset/www/ before it, so it can be used on android devices. The complete functionality is wrapped inside the $ionicPlatform.ready() function to assure that everything is loaded before the plugin is used. After that, we are creating the media object by using the newMedia(src) method. The media object is used for adding play, pause, stop and release functionalities. Controller Code .controller(”MyCtrl”, function($scope, $ionicPlatform, $cordovaMedia) { $ionicPlatform.ready(function() { var src = “/android_asset/www/js/song.mp3″; var media = $cordovaMedia.newMedia(src); $scope.playMedia = function() { media.play(); }; $scope.pauseMedia = function() { media.pause(); }; $scope.stopMedia = function() { media.stop(); }; $scope.$on(”destroy”, function() { media.release(); }); }); } We will also create three buttons for calling play, pause and stop functions. <button class = “button” ng-click = “playMedia()”>PLAY</button> <button class = “button” ng-click = “pauseMedia()”>PAUSE</button> <button class = “button” ng-click = “stopMedia()”>STOP</button> We need to run it on an emulator or a mobile device for this plugin to work. When the user’s tap on the play button, the song.mp3 will start playing. You can see in the above example that we use src as an option parameter. There are other optional parameters that can be used for the newMedia method. Optional Parameters The following table will show all the optional parameters available. Parameter Type Details mediaSuccess function Called after current play/record or stop action has completed. mediaError function Invoked when there is an error. mediaStatus function Invoked to show status changes. The next table will show all the methods available. Available Methods The following table will show all the methods available. Method Parameters Details newMedia(parameter1) src Returns media object that will be used for future methods. src is an URI of the audio content. getCurrentPosition / Returns the current position within an audio file. getDuration / Returns the duration of an audio file. play / Used to start or resume playing. pause / Used to pause playback. stop / Used to stop playing. release / Used to release audio resources. seekTo(parameter1) milliseconds Used to set the playback position in milliseconds. setVolume(parameter1) volume Used to change volume. Range is from 0 to 1 startRecord() / Used to start recording. stopRecord / Used to stop recording. Print Page Previous Next Advertisements ”;
Category: ionic
Ionic – JS Backdrop
Ionic – Javascript Backdrop ”; Previous Next The Ionic Backdrop will overlay the content of the screen when applied. It will appear below other overlays (popup, loading, etc…). There are two methods that can be used for managing backdrop service. The $ionicBackdrop.retain() will apply backdrop over the components, and $ionicBackdrop.release() will remove it. Using Backdrop The following example shows how to use backdrop. We are adding $ionicBackdrop as a dependency to the controller, then creating the $scope.showBackdrop() function that will call the retain method immediately. Then, after three seconds, it will call the release method. We are using $timeout for the release method, so we need to add it as a controller dependency too. .controller(”myCtrl”, function($scope, $ionicBackdrop, $timeout) { $scope.showBackdrop = function() { $ionicBackdrop.retain(); $timeout(function() { $ionicBackdrop.release(); }, 3000); }; }) You will notice how the screen is darker in the following image, since the backdrop is applied. Print Page Previous Next Advertisements ”;
Ionic – Icons
Ionic – Icons ”; Previous Next There are more than 700 premium icons provided by Ionic. There are also different sets of icons provided for Android and IOS. You can find almost anything you need but you are not bound to use them, if you do not want to. You can use your own custom icons or any other icon set instead. You can check all the Ionic icons here. How to use Icons? If you want to use Ionic icons find the icon you need on the page (https://ionicons.com/). When you are adding Ionic elements, you always add the main class first and then you add the subclass you want. The main class for all icons is icon. The Subclass is the name of the icon you want. We will add six icons in our example that is given below − <i class = “icon icon ion-happy-outline”></i> <i class = “icon icon ion-star”></i> <i class = “icon icon ion-compass”></i> <i class = “icon icon ion-planet”></i> <i class = “icon icon ion-ios-analytics”></i> <i class = “icon icon ion-ios-eye”></i> The above code will produce the following screen − The size of these icons can be changed with the font-size property in your Ionic CSS file. .icon { font-size: 50px; } Once the icon size is setup, the same code will produce the following screenshot as the output − Default Icons Code Result <i class=”icon ion-ionic”></i> <i class=”icon ion-arrow-up-a”></i> <i class=”icon ion-arrow-right-a”></i> <i class=”icon ion-arrow-down-a”></i> <i class=”icon ion-arrow-left-a”></i> <i class=”icon ion-arrow-up-b”></i> <i class=”icon ion-arrow-right-b”></i> <i class=”icon ion-arrow-down-b”></i> <i class=”icon ion-arrow-left-b”></i> <i class=”icon ion-arrow-up-c”></i> <i class=”icon ion-arrow-right-c”></i> <i class=”icon ion-arrow-down-c”></i> <i class=”icon ion-arrow-left-c”></i> <i class=”icon ion-arrow-return-right”></i> <i class=”icon ion-arrow-return-left”></i> <i class=”icon ion-arrow-swap”></i> <i class=”icon ion-arrow-shrink”></i> <i class=”icon ion-arrow-expand”></i> <i class=”icon ion-arrow-move”></i> <i class=”icon ion-arrow-resize”></i> <i class=”icon ion-chevron-up”></i> <i class=”icon ion-chevron-right”></i> <i class=”icon ion-chevron-down”></i> <i class=”icon ion-chevron-left”></i> <i class=”icon ion-navicon-round”></i> <i class=”icon ion-navicon”></i> <i class=”icon ion-drag”></i> <i class=”icon ion-log-in”></i> <i class=”icon ion-log-out”></i> <i class=”icon ion-checkmark-round”></i> <i class=”icon ion-checkmark”></i> <i class=”icon ion-checkmark-circled”></i> <i class=”icon ion-close-round”></i> <i class=”icon ion-close”></i> <i class=”icon ion-close-circled”></i> <i class=”icon ion-plus-round”></i> <i class=”icon ion-plus”></i> <i class=”icon ion-plus-circled”></i> <i class=”icon ion-minus-round”></i> <i class=”icon ion-minus”></i> <i class=”icon ion-minus-circled”></i> <i class=”icon ion-information”></i> <i class=”icon ion-informaticon ion-circled”></i> <i class=”icon ion-help”></i> <i class=”icon ion-help-circled”></i> <i class=”icon ion-backspace-outline”></i> <i class=”icon ion-backspace”></i> <i class=”icon ion-help-buoy”></i> <i class=”icon ion-asterisk”></i> <i class=”icon ion-alert”></i> <i class=”icon ion-alert-circled”></i> <i class=”icon ion-refresh”></i> <i class=”icon ion-loop”></i> <i class=”icon ion-shuffle”></i> <i class=”icon ion-home”></i> <i class=”icon ion-search”></i> <i class=”icon ion-flag”></i> <i class=”icon ion-star”></i> <i class=”icon ion-heart”></i> <i class=”icon ion-heart-broken”></i> <i class=”icon ion-gear-a”></i> <i class=”icon ion-gear-b”></i> <i class=”icon ion-toggle-filled”></i> <i class=”icon ion-toggle”></i> <i class=”icon ion-settings”></i> <i class=”icon ion-wrench”></i> <i class=”icon ion-hammer”></i> <i class=”icon ion-edit”></i> <i class=”icon ion-trash-a”></i> <i class=”icon ion-trash-b”></i> <i class=”icon ion-document”></i> <i class=”icon ion-document-text”></i> <i class=”icon ion-clipboard”></i> <i class=”icon ion-scissors”></i> <i class=”icon ion-funnel”></i> <i class=”icon ion-bookmark”></i> <i class=”icon ion-email”></i> <i class=”icon ion-email-unread”></i> <i class=”icon ion-folder”></i> <i class=”icon ion-filing”></i> <i class=”icon ion-archive”></i> <i class=”icon ion-reply”></i> <i class=”icon ion-reply-all”></i> <i class=”icon ion-forward”></i> <i class=”icon ion-share”></i> <i class=”icon ion-paper-airplane”></i> <i class=”icon ion-link”></i> <i class=”icon ion-paperclip”></i> <i class=”icon ion-compose”></i> <i class=”icon ion-briefcase”></i> <i class=”icon ion-medkit”></i> <i class=”icon ion-at”></i> <i class=”icon ion-pound”></i> <i class=”icon ion-quote”></i> <i class=”icon ion-cloud”></i> <i class=”icon ion-upload”></i> <i class=”icon ion-more”></i> <i class=”icon ion-grid”></i> <i class=”icon ion-calendar”></i> <i class=”icon ion-clock”></i> <i class=”icon ion-compass”></i> <i class=”icon ion-pinpoint”></i> <i class=”icon ion-pin”></i> <i class=”icon ion-navigate”></i> <i class=”icon ion-location”></i> <i class=”icon ion-map”></i> <i class=”icon ion-lock-combination”></i> <i class=”icon ion-locked”></i> <i class=”icon ion-unlocked”></i> <i class=”icon ion-key”></i> <i class=”icon ion-arrow-graph-up-right”></i> <i class=”icon ion-arrow-graph-down-right”></i> <i class=”icon ion-arrow-graph-up-left”></i> <i class=”icon ion-arrow-graph-down-left”></i> <i class=”icon ion-stats-bars”></i> <i class=”icon ion-connecticon ion-bars”></i> <i class=”icon ion-pie-graph”></i> <i class=”icon ion-chatbubble”></i> <i class=”icon ion-chatbubble-working”></i> <i class=”icon ion-chatbubbles”></i> <i class=”icon ion-chatbox”></i> <i class=”icon ion-chatbox-working”></i> <i class=”icon ion-chatboxes”></i> <i class=”icon ion-person”></i> <i class=”icon ion-person-add”></i> <i class=”icon ion-person-stalker”></i> <i class=”icon ion-woman”></i> <i class=”icon ion-man”></i> <i class=”icon ion-female”></i> <i class=”icon ion-male”></i> <i class=”icon ion-transgender”></i> <i class=”icon ion-fork”></i> <i class=”icon ion-knife”></i> <i class=”icon ion-spoon”></i> <i class=”icon ion-soup-can-outline”></i> <i class=”icon ion-soup-can”></i> <i class=”icon ion-beer”></i> <i class=”icon ion-wineglass”></i> <i class=”icon ion-coffee”></i> <i class=”icon ion-icecream”></i> <i class=”icon ion-pizza”></i> <i class=”icon ion-power”></i> <i class=”icon ion-mouse”></i> <i class=”icon ion-battery-full”></i> <i class=”icon ion-battery-half”></i> <i class=”icon ion-battery-low”></i> <i class=”icon ion-battery-empty”></i> <i class=”icon ion-battery-charging”></i> <i class=”icon ion-wifi”></i> <i class=”icon ion-bluetooth”></i> <i class=”icon ion-calculator”></i> <i class=”icon ion-camera”></i> <i class=”icon ion-eye”></i> <i class=”icon ion-eye-disabled”></i> <i class=”icon ion-flash”></i> <i class=”icon ion-flash-off”></i> <i class=”icon ion-qr-scanner”></i> <i class=”icon ion-image”></i> <i class=”icon ion-images”></i> <i class=”icon ion-wand”></i> <i class=”icon ion-contrast”></i> <i class=”icon ion-aperture”></i> <i class=”icon ion-crop”></i> <i class=”icon ion-easel”></i> <i class=”icon ion-paintbrush”></i> <i class=”icon ion-paintbucket”></i> <i class=”icon ion-monitor”></i> <i class=”icon ion-laptop”></i> <i class=”icon ion-ipad”></i> <i class=”icon ion-iphone”></i> <i class=”icon ion-ipod”></i> <i class=”icon ion-printer”></i> <i class=”icon ion-usb”></i> <i class=”icon ion-outlet”></i> <i class=”icon ion-bug”></i> <i class=”icon ion-code”></i> <i class=”icon ion-code-working”></i> <i class=”icon ion-code-download”></i> <i class=”icon ion-fork-repo”></i> <i class=”icon ion-network”></i> <i class=”icon ion-pull-request”></i> <i class=”icon ion-merge”></i> <i class=”icon ion-xbox”></i> <i class=”icon ion-playstation”></i> <i class=”icon ion-steam”></i> <i class=”icon ion-closed-captioning”></i> <i class=”icon ion-videocamera”></i> <i class=”icon ion-film-marker”></i> <i class=”icon ion-disc”></i> <i class=”icon ion-headphone”></i> <i class=”icon ion-music-note”></i> <i class=”icon ion-radio-waves”></i> <i class=”icon ion-speakerphone”></i> <i class=”icon ion-mic-a”></i> <i class=”icon ion-mic-b”></i> <i class=”icon ion-mic-c”></i> <i class=”icon ion-volume-high”></i> <i class=”icon ion-volume-medium”></i> <i class=”icon ion-volume-low”></i> <i class=”icon ion-volume-mute”></i> <i class=”icon ion-levels”></i> <i class=”icon ion-play”></i> <i class=”icon ion-pause”></i> <i class=”icon ion-stop”></i> <i class=”icon ion-record”></i> <i class=”icon ion-skip-forward”></i> <i class=”icon ion-skip-backward”></i> <i class=”icon ion-eject”></i> <i class=”icon ion-bag”></i> <i class=”icon ion-card”></i> <i class=”icon ion-cash”></i> <i class=”icon ion-pricetag”></i> <i class=”icon ion-pricetags”></i> <i class=”icon ion-thumbsup”></i> <i class=”icon ion-thumbsdown”></i> <i class=”icon ion-happy-outline”></i> <i class=”icon ion-happy”></i> <i class=”icon ion-sad-outline”></i> <i class=”icon ion-sad”></i> <i class=”icon ion-bowtie”></i> <i class=”icon ion-tshirt-outline”></i> <i class=”icon ion-tshirt”></i> <i class=”icon ion-trophy”></i> <i class=”icon ion-podium”></i> <i class=”icon ion-ribbon-a”></i> <i class=”icon ion-ribbon-b”></i> <i class=”icon ion-university”></i> <i class=”icon ion-magnet”></i> <i class=”icon ion-beaker”></i> <i class=”icon ion-erlenmeyer-flask”></i> <i class=”icon ion-egg”></i> <i class=”icon ion-earth”></i> <i class=”icon ion-planet”></i> <i class=”icon ion-lightbulb”></i> <i class=”icon ion-cube”></i> <i class=”icon ion-leaf”></i> <i class=”icon ion-waterdrop”></i> <i class=”icon ion-flame”></i> <i class=”icon ion-fireball”></i> <i class=”icon ion-bonfire”></i> <i class=”icon ion-umbrella”></i> <i class=”icon ion-nuclear”></i> <i class=”icon ion-no-smoking”></i> <i class=”icon ion-thermometer”></i> <i class=”icon ion-speedometer”></i> <i class=”icon ion-model-s”></i> <i class=”icon ion-plane”></i> <i class=”icon ion-jet”></i> <i class=”icon ion-load-a”></i> <i class=”icon ion-load-b”></i> <i class=”icon ion-load-c”></i> <i class=”icon ion-load-d”></i> iOS Style Icons Code Result <i class=”icon
Ionic – JS Scroll
Ionic – JavaScript Scroll ”; Previous Next The element used for scrolling manipulation in ionic apps is called as the ion-scroll. Using Scroll The following code snippets will create scrollable containers and adjust scrolling patterns. First, we will create our HTML element and add properties to it. We will add → direction = “xy” to allow scrolling to every side. We will also set the width and the height for the scroll element. HTML Code <ion-scroll zooming = “true” direction = “xy” style = “width: 320px; height: 500px”> <div class = “scroll-container”></div> </ion-scroll> Next, we will add the image of our world map to div element, which we created inside the ion-scroll and set its width and height. CSS Code .scroll-container { width: 2600px; height: 1000px; background: url(”../img/world-map.png”) no-repeat } When we run our app, we can scroll the map in every direction. The following example shows the North America part of the map. We can scroll this map to any part that we want. Let us scroll it to show Asia. There are other attributes, which can be applied to the ion-scroll. You can check them in the following table. Scroll Attributes Attribute Type Details direction string Possible directions of the scroll. Default value is y delegate-handle string Used for scroll identification with $ionicScrollDelegate. locking boolean Used to lock scrolling in one direction at a time. Default value is true. paging boolean Used to determine if the paging will be used with scroll. on-refresh expression Called on pull-to-refresh. on-scroll expression Called when scrolling. scrollbar-x boolean Should horizontal scroll bar be shown. Default value is true. scrollbar-y string Should vertical scroll bar be shown. Default value is true. zooming boolean Used to apply pinch-to-zoom. min-zoom integer Minimal zoom value. max-zoom integer Maximal zoom value. scrollbar-x boolean Used to enable bouncing. Default value on IOS is true, on Android false. Infinite Scroll An Infinite scroll is used to trigger some behavior when scrolling passes the bottom of the page. The following example shows how this works. In our controller, we created a function for adding items to the list. These items will be added when a scroll passes 10% of the last element loaded. This will continue until we hit 30 loaded elements. Every time loading is finished, on-infinite will broadcast scroll.infiniteScrollComplete event. HTML Code <ion-list> <ion-item ng-repeat = “item in items” item = “item”>Item {{ item.id }}</ion-item> </ion-list> <ion-infinite-scroll ng-if = “!noMoreItemsAvailable” on-infinite = “loadMore()” distance = “10%”></ion-infinite-scroll> Controller Code .controller(”MyCtrl”, function($scope) { $scope.items = []; $scope.noMoreItemsAvailable = false; $scope.loadMore = function() { $scope.items.push({ id: $scope.items.length}); if ($scope.items.length == 30) { $scope.noMoreItemsAvailable = true; } $scope.$broadcast(”scroll.infiniteScrollComplete”); }; }) Other attributes can also be used with ion-infinite-scroll. Some of them are listed in the table below. Scroll Attributes Attribute Type Details on-infinite expression What should be called when scrolled to the bottom. distance string The distance from the bottom needed to trigger on-infinite expression. spinner string What spinner should be shown while loading immediate-check Boolean Should ‘on-infinite’ be called when screen is loaded Scroll Delegate Ionic offers delegate for full control of the scroll elements. It can be used by injecting a $ionicScrollDelegate service to the controller, and then use the methods it provides. The following example shows a scrollable list of 20 objects. HTML Code <div class = “list”> <div class = “item”>Item 1</div> <div class = “item”>Item 2</div> <div class = “item”>Item 3</div> <div class = “item”>Item 4</div> <div class = “item”>Item 5</div> <div class = “item”>Item 6</div> <div class = “item”>Item 7</div> <div class = “item”>Item 8</div> <div class = “item”>Item 9</div> <div class = “item”>Item 10</div> <div class = “item”>Item 11</div> <div class = “item”>Item 12</div> <div class = “item”>Item 13</div> <div class = “item”>Item 14</div> <div class = “item”>Item 15</div> <div class = “item”>Item 16</div> <div class = “item”>Item 17</div> <div class = “item”>Item 18</div> <div class = “item”>Item 19</div> <div class = “item”>Item 20</div> </div> <button class = “button” ng-click = “scrollTop()”>Scroll to Top!</button> Controller Code .controller(”DashCtrl”, function($scope, $ionicScrollDelegate) { $scope.scrollTop = function() { $ionicScrollDelegate.scrollTop(); }; }) The above code will produce the following screen − When we tap the button, the scroll will be moved to the top. Now, we will go through all of the $ionicScrollDelegate methods. Delegate Methods Method Parameters Type Details scrollTop(parameter) shouldAnimate boolean Should scroll be animated. scrollBottom(parameter) shouldAnimate boolean Should scroll be animated. scrollTo(parameter1, parameter2, parameter3) left, top, shouldAnimate number, number, integer First two parameters determine value of the x, and y-axis offset. scrollBy(parameter1, parameter2, parameter3) left, top, shouldAnimate number, number, integer First two parameters determine value of the x, and y-axis offset. zoomTo(parameter1, parameter2, parameter3, parameter4) level, animate, originLeft, originTop number, boolean, number, number level is used to determine level to zoom to. originLeft and originRight coordinates where the zooming should happen. zoomBy(parameter1, parameter2, parameter3, parameter4) factor, animate, originLeft, originTop number, boolean, number, number factor is used to determine factor to zoom by. originLeft and originRight coordinates where the zooming should happen. getScrollPosition() / / Returns object with two number as properties: left and right. These numbers represent the distance the user has scrolled from the left and from the top respectively. anchorScroll(parameter1) shouldAnimate boolean It will scroll to the element with the same id as the window.loaction.hash. If this element does not exist, it will scroll to the top. freezeScroll(parameter1) shouldFreeze boolean Used to disable scrolling for particular scroll. freezeAllScrolls(parameter1) shouldFreeze boolean Used to disable scrolling for all the scrolls in the app. getScrollViews() / object Returns the scrollView object. $getByHandle(parameter1) handle string Used to connect methods to the particular scroll view with the same handle. $ionicScrollDelegate. $getByHandle(”my-handle”).scrollTop(); Print Page Previous Next Advertisements ”;
Ionic – JS Navigation
Ionic – Javascript Navigation ”; Previous Next Navigation is one of the core components of every app. Ionic is using the AngularJS UI Router for handling the navigation. Using Navigation Navigation can be configured in the app.js file. If you are using one of the Ionic templates, you will notice the $stateProvider service injected into the app config. The simplest way of creating states for the app is showed in the following example. The $stateProvider service will scan the URL, find the corresponding state and load the file, which we defined in app.config. app.js Code .config(function($stateProvider) { $stateProvider .state(”index”, { url: ”/”, templateUrl: ”templates/home.html”}) .state(”state1”, {url: ”/state1”, templateUrl: ”templates/state1.html”}) .state(”state2”, {url: ”/state2”, templateUrl: ”templates/state2.html”,}); }); The state will be loaded into the ion-nav-view element, which can be placed in the index.html body. index.html Code <ion-nav-view></ion-nav-view> When we created states in the above-mentioned example, we were using the templateUrl, so when the state is loaded, it will search for matching the template file. Now, we will open the templates folder and create a new file state1.html, which will be loaded when the app URL is changed to /state1. state1.html Code <ion-view> <ion-content> This is State 1 !!! </ion-content> </ion-view> Creating Navigation Menu You can add a navigation bar to your app in the index.html body by adding the “ion-nav-bar” element. Inside the navigation bar, we will add the ion-nav-back-button with an icon. This will be used for returning to the previous state. The button will appear automatically when the state is changed. We will assign the goBack() function, which will use the $ionicHistory service for handling this functionality. Therefore, when the user leaves the home state and goes to state1, the back button will appear which can be taped, if the user wants to return to the home state. index.html Code <ion-nav-bar class = “bar-positive”> <ion-nav-back-button class = “button-clear” ng-click = “goBack()”> <i class = “icon ion-arrow-left-c”></i> Back </ion-nav-back-button> </ion-nav-bar> Controller Code .MyCtrl($scope, $ionicHistory) { $scope.goBack = function() { $ionicHistory.goBack(); }; } Adding Navigation Elements Buttons can be added to the navigation bar using the ion-nav-buttons. This element should be placed inside the ion-nav-bar or the ion-view. We can assign the side attribute with four option values. The primary and secondary values will place buttons according to the platform that is used. Sometimes you want the buttons on one side no matter if it is IOS or Android. If that is the case, you can use the left or the right attributes instead. We can also add the ion-nav-title to the navigation bar. All the code will be placed in the index.html body, so it can be used everywhere. <ion-nav-bar class = “bar-positive”> <ion-nav-title> Title </ion-nav-title> <ion-nav-buttons side = “primary”> <button class = “button”> Button 1 </button> </ion-nav-buttons> </ion-nav-bar> It will produce the following screen − Other Navigation Attributes The following table shows a few other functionalities, which can be used with Ionic navigation. Navigation Attributes Attribute Options Detail nav-transition none, iOS, Android Used to set animation that should be applied when transition occurs. nav-direction forward, back, enter, exit, swap Used to set the direction of the animation when transition occurs. hardwareBackButtonClose Boolean It will enable closing the modal when hardware back button is clicked. Default value is true. Caching Ionic has the ability for caching up to ten views to improve performance. It also offers a way to handle caching manually. Since only backward views are cached and the forward ones are loading each time the users visit them, we can easily set to cache forward views by using following the code. $ionicCinfigProvider.views.forwardCache(true); We can also set how many states should be cached. If we want three views to be cached, we can use the following code. $ionicConfigProvider.views.maxCache(3); Caching can be disabled inside $stateProvider or by setting attribute to ion-view. Both examples are below. $stateProvider.state(”state1”, { cache: false, url : ”/state1”, templateUrl: ”templates/state1.html” }) <ion-view cache-view = “false”></ion-view> Controlling the Navigation Bar We can control the behavior of the navigation bar by using the $ionicNavBarDelegate service. This service needs to be injected to our controller. HTML Code <ion-nav-bar> <button ng-click = “setNavTitle(”title”)”> Set title to banana! </button> </ion-nav-bar> Controller Code $scope.setNavTitle = function(title) { $ionicNavBarDelegate.title(title); } The $ionicNavBarDelegate service has other useful methods. Some of these methods are listed in the following table. Methods for $ionicNavBarDelegate Method Parameter Type Detail align(parameter) center, left, right string Used to align the title. showBackButton(parameter) show Boolean Used to show or hide the back button. title(parameter) title string Used to show the new title. Tracking History You can track the history of the previous, current and the forward views by using the $ionicHistory service. The following table shows all the methods of this service. Methods for $ionicHistory Method Parameter Type Detail viewHistory / object Returns the app view history data. currentView() / object Returns the current view. title(parameter) title string Returns the ID of the view which is parent of the current view. currentTitle(parameter) val string Returns the title of the current view. It can be updated by setting new val value. backView() / string Returns the last back view. backTitle() / string Returns the title of last back view. forwardView() / object Returns the last forward view. currentStateName() / string Returns the current state name. goBack() backCount number Used to set how many views to go back. Number should be negative. If it is positive or zero it will have no effect. clearHistory() / / Used to clear entire view history. clearCache() / promise Used to clear all cached views. nextViewOptions() / object Sets the options of the next view. You can look the following example for more info. The nextViewOptions() method has the following three options available. disableAnimate is used for disabling animation of the next view change. disableBack will set the back view to null. historyRoot will set the next view as the root view. $ionicHistory.nextViewOptions({ disableAnimate: true, disableBack: true }); Print Page Previous Next
Ionic – Cards
Ionic – Cards ”; Previous Next Since mobile devices have smaller screen size, cards are one of the best elements for displaying information that will feel user friendly. In the previous chapter, we have discussed how to inset lists. Cards are very similar to inset lists, but they offer some additional shadowing that can influence the performance for larger lists. Adding Cards A default card can be created by adding a card class to your element. Cards are usually formed as lists with the item class. One of the most useful class is the item-text-wrap. This will help when you have too much text, so you want to wrap it inside your card. The first card in the following example does not have the item-text-wrap class assigned, but the second one is using it. <div class = “card”> <div class = “item”> This is a Ionic card without item-text-wrap class. </div> <div class = “item item-text-wrap”> This is a Ionic card with item-text-wrap class. </div> </div> The above code will produce the following screen − Card Header and Footer In the previous chapter, we have already discussed how to use the item-divider class for grouping lists. This class can be very useful when working with cards to create card headers. The same class can be used for footers as shown in the following code − <div class = “card list”> <div class = “item item-divider”> Card header </div> <div class = “item item-text-wrap”> Card text… </div> <div class = “item item-divider”> Card Footer </div> </div> The above code will produce the following screen − Complete Card You can add any element on top of your card. In following example, we will show you how to use the full-image class together with the item-body to get a good-looking windowed image inside your card. <div class = “card”> <div class = “item item-avatar”> <img src = “my-image.png”> <h2>Card Name</h2> </div> <div class = “item item-body”> <img class = “full-image” src = “my-image.png”> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget pharetra tortor. Proin quis eros imperdiet, facilisis nisi in, tincidunt orci. Nam tristique elit massa, quis faucibus augue finibus ac. </div> </div> The above code will produce the following screen − Print Page Previous Next Advertisements ”;
Ionic – JS Side Menu
Ionic – JavaScript Side Menu ”; Previous Next Side menu is one of the most used Ionic components. The Side menu can be opened by swiping to the left or right or by triggering the button created for that purpose. Using Side Menu The first element that we need is ion-side-menus. This element is used for connecting the side menu with all the screens that will use it. The ion-side-menu-content element is where the content will be placed and the ion-side-menu element is the place where we can put a side directive. We will add the side menu to the index.html and place the ion-nav-view inside the side menu content. This way the side menu can be used throughout entire app. index.html <ion-side-menus> <ion-side-menu>side = “left”> <h1>SIde Menu</h1> </ion-side-menu> <ion-side-menu-content> <ion-nav-view> </ion-nav-view> </ion-side-menu-content> </ion-side-menus> Now, we will create button with menu-toggle = “left” directive. This button will usually be placed in the apps header bar, but we will add it in our template file for better understanding. When the button is tapped or when we swipe to the right, the side menu will open. You could also set the menu-close directive, if you would like to have one button only for closing side menu, but we will use the toggle button for this. HTML Template <button menu-toggle = “left” class = “button button-icon icon ion-navicon”></button> The above code will produce the following screen − You can add some additional attributes to the ion-side-menus element. The enable-menu-with-back-views can be set to false to disable side menu, when the back button is showed. This will also hide the menu-toggle button from the header. The other attribute is delegate-handle, which will be used for the connection with $ionicSideMenuDelegate. The ion-side-menu-content element can use its own attribute. When the drag-content attribute is set to false, it will disable the ability to open the side menu by swiping the content screen. The edge-drag-threshold attribute has a default value of 25. This means that swiping is allowed only 25 pixels from the left and right edge of the screen. We can change this number value or we can set it to false to enable swiping on the entire screen or true to disable it. The ion-side-menu can use the side attribute that we showed in the example above. It will determine whether the menu should appear from the left or the right side. The ‘is-enabled’ attribute with a false value will disable the side menu, and the width attribute value is a number that represents how wide the side menu should be. The default value is 275. Side Menu Delegate The $ionicSideMenuDelegate is a service used for controlling all the side menus in the app. We will show you how to use it, and then we will go through all the options available. Like all the Ionic services, we need to add it as a dependency to our controller and then use it inside the controller’s scope. Now, when we click the button, all of the side menus will open. Controller Code .controller(”MyCtrl”, function($scope, $ionicSideMenuDelegate) { $scope.toggleLeftSideMenu = function() { $ionicSideMenuDelegate.toggleLeft(); }; }) HTML Code <button class = “button button-icon icon ion-navicon” ng-click = “toggleLeft()”></button> The following table shows the $ionicScrollDelegate methods. Delegate Methods Method Parameters Type Details toggleLeft(parameter) isOpen Boolean Used for opening or closing side menu. toggleRight(parameter) isOpen Boolean Used for opening or closing side menu. getOpenRatio() / / Returns ratio of open part over menu width. If half of the menu is open from the left, the ration will be 0.5. If side menu is closed, it will return 0. If half of the menu is open from the right side, it will return -0.5. isOpen() / Boolean Returns true if side menu is open, false if it is closed. isOpenLeft() / Boolean Returns true if left side menu is open, false if it is closed. isOpenRight() / Boolean Returns true if right side menu is open, false if it is closed. getScrollPosition() / / Returns object with two number as properties: left and right. These numbers represent the distance the user has scrolled from the left and from the top respectively. canDragContent(parameter1) canDrag Boolean Whether the content can be dragged to open side menu. edgeDragThreshold(parameter1) value Boolean|number If the value is true, the side menu can be opened by dragging 25px from the edges of the screen. If it is false, dragging is disabled. We can set any number that will represent pixel value from the left and right edge of the screen. $getByHandle(parameter1) handle string Used to connect methods to the particular side menu view with the same handle. $ionicSideMenuDelegate. $getByHandle(”my-handle”).toggleLeft(); Print Page Previous Next Advertisements ”;
Ionic – Radio Button
Ionic – Radio Button ”; Previous Next Radio buttons are another form of an element, which we will be covering in this chapter. The difference between radio buttons from toggle and checkbox forms is that when using the former, you choose only one radio button from the list. As the latter allows you to choose more than one. Adding Radio Buttons Since there will always be more than one radio button to choose from, the best way is to create a list. We did this whenever we wanted multiple elements. The list item class will be item-radio. Again, we will use label for this as we have used with all the other forms. Input will have the name attribute. This attribute will group all the buttons that you want to use as a possible choice. The item-content class is used to display options clearly. At the end, we will use the radio-icon class to add the checkmark icon that will be used to mark the option that the user chooses. In the following example, there are four radio buttons and the second one is chosen. <div class = “list”> <label class = “item item-radio”> <input type = “radio” name = “group1” /> <div class = “item-content”> Choice 1 </div> <i class = “radio-icon ion-checkmark”></i> </label> <label class = “item item-radio”> <input type = “radio” name = “group1” /> <div class = “item-content”> Choice 2 </div> <i class = “radio-icon ion-checkmark”></i> </label> <label class = “item item-radio”> <input type = “radio” name = “group1” /> <div class = “item-content”> Choice 3 </div> <i class = “radio-icon ion-checkmark”></i> </label> <label class = “item item-radio”> <input type = “radio” name = “group1” /> <div class = “item-content”> Choice 4 </div> <i class = “radio-icon ion-checkmark”></i> </label> </div> The above code will produce the following screen − Multiple Radio Button Groups Sometimes you want to create more than one group. This is what the name attribute is made for; the following example will group the first two and the last two buttons as two option groups. We will use the item-divider class to separate two groups. Notice that the first group has the name attribute equal to group1 and the second one uses group2. <div class = “list”> <div class = ” item item-divider”> Group1 </div> <label class = “item item-radio”> <input type = “radio” name = “group1” /> <div class = “item-content”> Choice 1 </div> <i class = “radio-icon ion-checkmark”></i> </label> <label class = “item item-radio”> <input type = “radio” name = “group1” /> <div class = “item-content”> Choice 2 </div> <i class = “radio-icon ion-checkmark”></i> </label> <div class = “item item-divider”> Group2 </div> <label class = “item item-radio”> <input type = “radio” name = “group2” /> <div class = “item-content”> Choice 3 </div> <i class = “radio-icon ion-checkmark”></i> </label> <label class = “item item-radio”> <input type = “radio” name = “group2” /> <div class = “item-content”> Choice 4 </div> <i class = “radio-icon ion-checkmark”></i> </label> </div> The above code will produce the following screen − Print Page Previous Next Advertisements ”;
Ionic – Tabs
Ionic – Tabs ”; Previous Next Ionic tabs are most of the time used for mobile navigation. Styling is optimized for different platforms. This means that on android devices, tabs will be placed at the top of the screen, while on IOS it will be at the bottom. There are different ways of creating tabs. We will understand in detail, how to create tabs in this chapter. Simple Tabs Simple Tabs menu can be created with a tabs class. For the inside element that is using this class, we need to add tab-item elements. Since tabs are usually used for navigation, we will use <a> tags for tab items. The following example is showing a menu with four tabs. <div class = “tabs”> <a class = “tab-item”> Tab 1 </a> <a class = “tab-item”> Tab 2 </a> <a class = “tab-item”> Tab 3 </a> </div> The above code will produce the following screen − Adding Icons Ionic provides classes for adding icons to tabs. If you want your tabs to have icons without any text, a tabs-icon-only class should be added after the tabs class. Of course, you need to add icons you want to display. <div class = “tabs tabs-icon-only”> <a class = “tab-item”> <i class = “icon ion-home”></i> </a> <a class = “tab-item”> <i class = “icon ion-star”></i> </a> <a class = “tab-item”> <i class = “icon ion-planet”></i> </a> </div> The above code will produce the following screen − You can also add icons and text together. The tabs-icon-top and tabs-icon-left are classes that will place the icon above or on the left side respectively. Implementation is the same as the example given above, we will just add a new class and text that we want to use. The following example shows icons placed above the text. <div class = “tabs tabs-icon-top”> <a class = “tab-item”> <i class = “icon ion-home”></i> Tab 1 </a> <a class = “tab-item”> <i class = “icon ion-star”></i> Tab 2 </a> <a class = “tab-item”> <i class = “icon ion-planet”></i> Tab 3 </a> </div> The above code will produce the following screen − Striped Tabs Striped Tabs can be created by adding a container around our tabs with the tabs-striped class. This class allows the usage of the tabs-background and the tabs-color prefixes for adding some of the Ionic colors to the tabs menu. In the following example, we will use the tabs-background-positive (blue) class to style the background of our menu, and the tabs-color-light (white) class to style the tab icons. Notice the difference between the second tab that is active and the other two that are not. <div class = “tabs-striped tabs-background-positive tabs-color-light”> <div class = “tabs”> <a class = “tab-item”> <i class = “icon ion-home”></i> </a> <a class = “tab-item active”> <i class = “icon ion-star”></i> </a> <a class = “tab-item”> <i class = “icon ion-planet”></i> </a> </div> </div> The above code will produce the following screen − Print Page Previous Next Advertisements ”;
Ionic – Range
Ionic – Range ”; Previous Next Ionic range is used to choose and display the level of something. It will represent the actual value in co-relation to maximal and minimal value. Ionic offers a simple way of working with Range. Using Range Range is used as an inside item element. The class that is used is range. We will place this class after the item class. This will prepare a container where the range will be placed. After creating a container, we need to add input and assign the range type to it and the name attribute as well. <div class = “item range”> <input type = “range” name = “range1”> </div> The above code will produce the following screen − Adding Icons Range will usually require icons to display the data clearly. We just need to add icons before and after the range input to place them on both sides of the range element. <div class = “item range”> <i class = “icon ion-volume-low”></i> <input type = “range” name = “volume”> <i class = “icon ion-volume-high”></i> </div> The above code will produce the following screen − Styling Range Our next example will show you how to style Range with Ionic colors. The color classes will use a range prefix. We will create a list with nine ranges and style it differently. <div class = “list”> <div class = “item range range-light”> <input type = “range” name = “volume”> </div> <div class = “item range range-stable”> <input type = “range” name = “volume”> </div> <div class = “item range range-positive”> <input type = “range” name = “volume”> </div> <div class = “item range range-calm”> <input type = “range” name = “volume”> </div> <div class = “item range range-balanced”> <input type = “range” name = “volume”> </div> <div class = “item range range-energized”> <input type = “range” name = “volume”> </div> <div class = “item range range-assertive”> <input type = “range” name = “volume”> </div> <div class = “item range range-royal”> <input type = “range” name = “volume”> </div> <div class = “item range range-dark”> <input type = “range” name = “volume”> </div> </div> The above code will produce the following screen − Print Page Previous Next Advertisements ”;