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(); }; });
完整源码:
<html ng-app="ionicApp"> <head> <meta charset="utf-8"> <meta content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title></title> <link href="https://cdn.bootcss.com/ionic/1.0.1/css/ionic.min.css"> <script src="https://cdn.bootcss.com/ionic/1.0.1/js/ionic.bundle.min.js"></script> <style> body { cursor: url('http://ionicframework.com/img/finger.png'), auto; } </style> <script> 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(); }; }); </script> </head> <body ng-controller="AppCtrl"> <ion-header-bar> <h1>Contacts</h1> <div> <button 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 type="text/ng-template"> <ion-modal-view> <ion-header-bar> <h1>New Contact</h1> <button ng-click="modal.hide()">Cancel</button> </ion-header-bar> <ion-content> <div> <label> <span>First Name</span> <input ng-model="newUser.firstName" type="text"> </label> <label> <span>Last Name</span> <input ng-model="newUser.lastName" type="text"> </label> <label> <span>Email</span> <input ng-model="newUser.email" type="text"> </label> <button ng-click="createContact(newUser)">Create</button> </div> </ion-content> </ion-modal-view> </script> </body> </html>
您可能感兴趣的文章: