<script src="../angular.min.js"></script> <script src="mymodule.js"></script> </head> <body> <div ng-app="myModule" <div ng-controller="MyCtrl"> <p>Firstname: <input type="text" ng-model="model.firstname"></p> <p>Lastname: <input type="text" ng-model="model.lastname"></p> <p>{{model.firstname + " " + model.lastname}}</p> <button ng-click="click()">Show Number</button> </div> </div> </body> </html> This is now the model object from MyCtrl. Model object is shared with view and controller
// Service function can use "this" and the return // value is this. myApp.service('CustomerService', function() { this.contacts = [{name: "Jack", salary: 3000}, {name: "Tina", salary: 5000}, {name: "John", salary: 4000}]; });
/> <script src="../angular.min.js" type="text/javascript"></script> <script src="angular-route.min.js" type="text/javascript"></script> <script src="myapp.js" type="text/javascript"> </script> </head> <body> <div data-ng-view=""></div> </body> </html> The content of this will change dynamically We will have to load additional module
before this. var myApp = angular.module('myApp', ['ngRoute']); // Configure routing. myApp.config(function($routeProvider) { // Usually we have different controllers for different views. // In this demonstration, the controller does nothing. $routeProvider.when('/', { templateUrl: 'view1.html', controller: 'MySimpleCtrl' }); $routeProvider.when('/view2', { templateUrl: 'view2.html', controller: 'MySimpleCtrl' }); $routeProvider.otherwise({ redirectTo: '/' }); }); // Let's add a new controller to MyApp myApp.controller('MySimpleCtrl', function ($scope) { });
a web app in Browser. UI optimized for mobile • Benefit: only one application to be updated • Cons: cannot be sold in app stores • Hybrid • Native WebView component which opens preloaded web app (html5, js, css ..) • Benefit: can be sold in app stores, access to some native features • Cons: must be compiled for every platform • Native • Implemented using native development kit • Benefit: native UI + speed • Cons: must be developed for each platform separately
(NodeJS requirement) • npm install -g cordova ionic • Create project (cd project/) • ionic start myApp tabs|blank|sidemenu • Run it • ionic platform add android|ios • ionic build android|ios • Emulate • ionic emulate android|ios • Serve as web page • ionic serve • To be able to build for android, you must have android SDK preinstalled in your computer
path so you can use the SDK from command line • android sdk • configure platforms • android avd • configure virtual devices • adb install path/to/android-debug.apk • Install the apk to emulator or device • For speedier emulator, install HAXM or Genymotion
// Hide the accessory bar by default (remove this to show // the accessory bar above the keyboard // for form inputs) if(window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if(window.StatusBar) { StatusBar.styleDefault(); } }); }) starterModule.controller('MyCtrl', function($scope) { $scope.customers = [{name: 'jack'}, {name: 'tina'}]; });
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard // for form inputs) if(window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if(window.StatusBar) { StatusBar.styleDefault(); } }); }) starterModule.controller('MyCtrl', function($scope, $resource) { var result = $resource("http://api.randomuser.me/?results=20"); var root = result.get(function() { $scope.customers = root.results; }); });
app using web-technologies and wrap app inside of native code • Good if you want to develop for several mobile platforms • Able to access some of devices native features • Ionic uses Cordova
js and css • App executes inside of WebView – component of native platform • By using plugins, you can communicate from JS to native device features • http://cordova.apache.org/plugins/ • By default, no plugins in your project • Cordova does not provide UI components
Cordova API • Install inside of your Ionic project • bower install ngCordova • Include ng-cordova.min.js in your index.html before cordova.js • Inject as AngularJS depencency • angular.module('myApp', ['ngCordova'])