2016-11-11 10 views
2

私はアイテムのリストが単純で、ヘッダーに各リストアイテムの横にある削除ボタンを表示して非表示にするボタンが必要です。私のヘッダーとコンテンツは、別々のビューで構成されています。ヘッダービューのイオン削除ボタンリストビューの削除ボタンをトグルする

はるかに読み出した後、コントローラは、ビューではなく状態に取り付けられていると思われるので、私は、各ビュー(ヘッダーのための1つのコントローラと、コンテンツのための1つのコントローラ)のために別のコントローラを持っている必要があります。コントローラ間で変数を共有することはできないので、あるビュー(header.html)にボタンを表示する/表示しないボタンを別のビュー(content.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は以下の通りです..:

私のHTMLは以下の通りです...

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; 
    }; 

}); 

答えて

関連する問題