2016-11-11 120 views
2

我有一個簡單的項目列表,並且希望在標題中顯示並隱藏每個列表項旁邊的刪除按鈕。我的標題和內容由單獨的視圖組成。標題視圖中的離子刪除按鈕在列表視圖中切換刪除按鈕

經過多次閱讀,似乎控制器被附加到視圖而不是狀態,所以我需要爲每個視圖(一個控制器的標題和一個控制器的內容)有一個單獨的控制器。由於我無法在控制器之間共享變量,在一個視圖(header.html)中顯示按鈕的最佳方式是在不同視圖(content.html)中的列表中顯示/隱藏按鈕?

我的HTML低於:

的index.html

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
    <title></title> 

    <link href="lib/ionic/css/ionic.css" rel="stylesheet"> 
    <link href="css/style.css" rel="stylesheet"> 

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above 
    <link href="css/ionic.app.css" rel="stylesheet"> 
    --> 

    <!--For users deploying their apps to Windows 8.1 or Android Gingerbread, platformOverrided.js 
    will inject platform-specific code from the /merges folder --> 
    <script src="js/platformOverrides.js"></script> 

    <!-- ionic/angularjs js --> 
    <script src="lib/ionic/js/ionic.bundle.js"></script> 
    <script src="Scripts/angular-resource.min.js"></script> 

    <!-- cordova script (this will be a 404 during development) --> 
    <script src="cordova.js"></script> 

    <!-- your app's js --> 
    <script src="js/app.js"></script> 
    <script src="js/controllers.js"></script> 
    <script src="js/services.js"></script> 
    </head> 

    <body ng-app="starter"> 
    <ion-view view-title="Playlists"> 
     <div ui-view="header"></div> 
     <div ui-view="content"></div> 
    </ion-view> 
</body> 
</html> 

了header.html

<ion-header-bar class="bar-positive"> 
    <div class="buttons"> 
     <button class="button button-icon icon ion-ios-minus-outline" 
       ng-click="data.showDelete = !data.showDelete"></button> 
    </div> 
    <h1 class="title">my test app</h1> 
</ion-header-bar> 

content.html

<ion-content class="has-header has-footer" overflow-scroll="true"> 
    <ion-list show-delete="data.showDelete"> 
     <ion-item ng-repeat="movie in movies" href="#/home/{{movie.id}}"> 
      {{movie.title}} 
      <ion-delete-button class="ion-minus-circled" 
           ng-click="onItemDelete(movie)"> 
      </ion-delete-button> 
      <ion-option-button class="button-assertive" ui-sref="editMovie({id:movie.id})">Edit</ion-option-button> 
     </ion-item> 
    </ion-list> 
</ion-content> 

和我的JS低於.. ...

app.js

angular.module('starter', ['ionic', 'ngResource', 'starter.controllers', 'starter.services']) 

.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
     if (cordova.platformId === "ios" && window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     cordova.plugins.Keyboard.disableScroll(true); 

    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 
    }); 
}) 

.config(function ($stateProvider, $urlRouterProvider) { 

    // if none of the above states are matched, use this as the fallback 
    $urlRouterProvider.otherwise('/'); 

    $stateProvider 

    .state('home', { 
     url: '/', 
     views: { 
      'header': { 
       templateUrl: 'templates/header.html', 
       controller: 'headerCtrl' 
      }, 
      'content': { 
       templateUrl: 'templates/content.html', 
       controller: 'contentCtrl' 
      }, 
      'footer': { 
       templateUrl: 'templates/footer.html' 
      } 
     } 
    }); 

}); 

controllers.js

angular.module('starter.controllers', []) 

.controller('headerCtrl', function ($scope) { 

    $scope.showDelete = function() { 
     $scope.data.showDelete = !$scope.data.showDelete; 
    }; 

}) 

.controller('contentCtrl', function ($scope, $state, $stateParams, Movie) { 

    // populate list withg all items from database 
    $scope.movies = Movie.query(); 

    // handle delete button click 
    $scope.onItemDelete = function (movie) { 
     $scope.movies.splice($scope.movies.indexOf(movie), 1); 
     movie.$delete(); 
     $scope.data.showDelete = false; 
    }; 

}); 

回答