ionic 切换开关操作


以下实例中,通过切换不同开关 checked 显示不同的值,true 为打开,false 为关闭。

HTML 代码

  1. <ion-header-bar class="bar-positive">
  2.   <h1 class="title">开关切换</h1>
  3. </ion-header-bar>
  4.          
  5. <ion-content>
  6.   
  7.   <div class="list">
  8.     
  9.     <div class="item item-divider"> 
  10.       Settings
  11.     </div>
  12.     
  13.     <ion-toggle ng-repeat="item in settingsList"
  14.                 ng-model="item.checked" 
  15.                 ng-checked="item.checked">
  16.       {{ item.text }}
  17.     </ion-toggle>
  18.     
  19.     <div class="item">
  20.      <!-- 使用 pre 标签展示效果更美观 -->
  21.       <div ng-bind="settingsList | json"></div> 
  22.     </div>
  23.     
  24.     <div class="item item-divider"> 
  25.       Notifications
  26.     </div>
  27.     
  28.     <ion-toggle ng-model="pushNotification.checked"
  29.                 ng-change="pushNotificationChange()">
  30.       Push Notifications
  31.     </ion-toggle>
  32.     
  33.     <div class="item">
  34.      <!-- 使用 pre 标签展示效果更美观 -->
  35.       <div ng-bind="pushNotification | json"></div> 
  36.     </div>
  37.     
  38.     <ion-toggle toggle-class="toggle-assertive"
  39.                 ng-model="emailNotification"
  40.                 ng-true-value="Subscribed"
  41.                 ng-false-value="Unubscribed">
  42.       Newsletter
  43.     </ion-toggle>
  44.     
  45.     <div class="item">
  46.      <!-- 使用 pre 标签展示效果更美观 -->
  47.       <div ng-bind="emailNotification | json"></div> 
  48.     </div>
  49.     
  50.   </div>
  51.   
  52. </ion-content>

由于pre标签冲突,实例中的 pre 已替换为 div标签,具体可以在"尝试一下"中查看。

JavaScript 代码

  1. angular.module('ionicApp', ['ionic'])
  2.  
  3. .controller('MainCtrl', function($scope) {
  4.  
  5.   $scope.settingsList = [
  6.     { text: "Wireless", checked: true },
  7.     { text: "GPS", checked: false },
  8.     { text: "Bluetooth", checked: false }
  9.   ];
  10.  
  11.   $scope.pushNotificationChange = function() {
  12.     console.log('Push Notification Change', $scope.pushNotification.checked);
  13.   };
  14.   
  15.   $scope.pushNotification = { checked: true };
  16.   $scope.emailNotification = 'Subscribed';
  17.   
  18. });

css 代码:

  1. body {
  2.   cursor: url('http://www.shouce.ren/try/demo_source/finger.png'), auto;
  3. }