-1

I'm developing my first app on Ionic and i need share data in list page and when i click on item, i need to display item details in another single page

Currently i'm make this code:

.controller('CiudadesCtrl', function($scope, $state) {

    // SEXTA REGION
    // Accordeon para la lista de ciudades
    $scope.groups = [
    {
        id_ciudad: 61,
        name: "Santa Cruz",
        emprendedores: [{
            id_emprendedor: 611,
            nombre: "Cabañas El Salto",
            telefono: "+56912345678",
            servicio: "alojamiento"
        }]
    },
    {
        id_ciudad: 62,
        name: "Marchigue",
        emprendedores: [{
            id_emprendedor: 621,
            nombre: "Cabañas Las Luciérnagas",
            telefono: "+56912345678",
            servicio: "alojamiento"
        }]
    }
    ];
})

HTML for display list:

<ion-view view-title="" hide-nav-bar="false" hide-back-button="false">
    <ion-content class="int-regiones" ng-controller="CiudadesCtrl">
        <h1 class="col-100">Región de O'Higgins</h1>
        <div class="featured">
            <img src="img/mapa-region-6.jpg" alt="" />
        </div>
        <div class="content">
            <div class="list col-100">
                <ion-list>
                    <div ng-repeat="group in groups">
                        <ion-item class="item-stable" ng-click="toggleGroup(group)" ng-class="{active: isGroupShown(group)}">
                            <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
                            &nbsp;
                            {{group.name}}
                        </ion-item>
                        <ion-item ng-repeat="item in group.emprendedores" class="item-accordion" ng-show="isGroupShown(group)">
                            <a id="{{group.id}}" class="item item-avatar" href="#/app/emprendedor/{{item.id_emprendedor}}">
                                <img src="img/ico-{{item.servicio}}.png">
                                <h2>{{item.nombre}}</h2>
                                <span>{{item.telefono}}</span>
                            </a>
                        </ion-item>
                        <!-- -->
                    </div>
                </ion-list>
            </div>
        </div>
    </ion-content>
</ion-view>

Controller for single:

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



});

And HTML for single:

<ion-view view-title="Emprendedor">
  <ion-content ng-controller="CiudadCtrl">
    <h1>{{item.nombre}}</h1>
  </ion-content>
</ion-view>

My first idea, was a popup, but is very hard to code it :(

In advance, thanks

Carlos
  • 11
  • 1
  • 5

2 Answers2

2

You can use a service for this task

  .service('info', function() {
      var self = this;
      self.get = function() {
        return [ 
               { 
                   id_ciudad: 61, name: "Santa Cruz", emprendedores: 
                   [
                       { 
                           id_emprendedor: 611, 
                           nombre: "Cabañas El Salto", 
                           telefono: "+56912345678", 
                           servicio: "alojamiento" 
                       }
                    ] 
               }, 
               {
                  id_ciudad: 62, name: "Marchigue", emprendedores: 
                   [
                       { 
                           id_emprendedor: 621, 
                           nombre: "Cabañas Las Luciérnagas", 
                           telefono: "+56912345678", 
                           servicio: "alojamiento" 
                       }
                   ]
                } 
          ]; //here is your groups ad example
      }
   });

 .controller('CiudadCtrl', function($scope, $state, $stateParams, info) {
     $scope.groups = info.get();
 });

 .controller('CiudadesCtrl', function($scope, $state, info) {
     $scope.groups = info.get();
 })
rkalita
  • 575
  • 4
  • 16
0

You need to have a service that returns the array of objects and a single item. A Once done, you need to collect by parameters id and send it to the service so that it will return the item in particular. Now you have the item in single view.

Controller for single:

app.controller('CiudadCtrl', function($scope, $stateParams, groupService) {
    var itemId = $stateParams.id;

    $scope.item = groupService.GetItem(itemId);
});

Service (factory):

app.factory('groupService', function() {
  var items = [{
    id_ciudad: 61,
    name: "Santa Cruz",
    emprendedores: [{
      id_emprendedor: 611,
      nombre: "Cabañas El Salto",
      telefono: "+56912345678",
      servicio: "alojamiento"
    }]
  }, {
    id_ciudad: 62,
    name: "Marchigue",
    emprendedores: [{
      id_emprendedor: 621,
      nombre: "Cabañas Las Luciérnagas",
      telefono: "+56912345678",
      servicio: "alojamiento"
    }]
  }];
  var itemsLength;
  var emprendedoresLength;

  return {
    GetItems: function() {
      return items;
    },
    GetItem: function(itemId) {
      itemsLength = items.length;
      for (i = 0; i < itemsLength; i++) {
        emprendedoresLength = items[i].emprendedores.length;
        for (x = 0; x < emprendedoresLength; x++) {
          if (items[i].emprendedores[x].id_emprendedor == itemId) {
            return items[i].emprendedores[x];
          }
        }
      }
    }
  };
});

See this Plunker: http://plnkr.co/edit/vFJte00JJkDGmbrqJt1d?p=preview