2

I have a simple list of items and want to have a button in the header that shows and hides a delete button next to each list item. My header and content are made up of separate views.

After much reading, it seems a controller is attached to a view rather than a state, so I need to have a separate controller for each view (one controller for the header and one controller for the content). As I can't share variables between controllers, what is the best way to have a button in one view (header.html) show/hide buttons in a list in a different view (content.html)?

My HTML is below:

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>

and my js is below.....

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

});
halfer
  • 19,824
  • 17
  • 99
  • 186
Damian
  • 1,652
  • 4
  • 26
  • 44

1 Answers1

0

You actually can share variables between controllers, by using what Angular calls a "service".

AngularJS: How can I pass variables between controllers?

Community
  • 1
  • 1