mhyder1
mhyder1

Reputation: 107

Passing data from Angular Material list to a new view

I'm working on a simple application that replicated the behavior of a typical Android app. Right now there is no back end, I'm just using an object array in my controller. I have a basic list with a few items in it. When the list is clicked I want the array of the index as well as the details to be passed to a details page. This is the list.

<md-list-item class="md-3-line" ng-repeat="it in ec.events" ng-href="#/details">
            <!--<md-button ng-click="ul.selectUser(it)" ng-class="{'selected' : it === ul.selected }">-->
            <div class="md-list-item-text" layout="column" style="margin:5px 0;">
                <!--<md-icon md-svg-icon="{{it.avatar}}" class="avatar"></md-icon>-->
                <h3>{{it.description}}</h3>
                <p><span style="color:black">Chapter: </span>{{it.chapter}}</p>
                <p><span style="color:black">Date: </span>{{it.date}}</p>
                <p><span style="color:black">Type: </span>{{it.type}}</p>
                <p><span style="color:black">Days: </span>{{it.outing_days}}</p>
            </div>
            <md-divider></md-divider>
        </md-list-item>

Right now I have and ng-href with a link to details but I need to pass the list info along to my new view. Here is the basics of the details page

<md-content flex layout-padding>
  <p>{{description}}</p>
  <p>{{chapter}}</p>
  <p>{{type}}</p>
  <p>{{date}}</p>
<md-content>

Both views share the same controller which contains a basic object array.

controller('AppCtrl',function($scope){
$scope.events = [
{description:'description1',date:'07/25/2016',type:'music',chapter:'Arizona'},
{description:'description2',date:'08/15/2016',type:'movie',chapter:'Texas'}
]
});

Upvotes: 0

Views: 97

Answers (1)

camden_kid
camden_kid

Reputation: 12813

Here you go - CodePen

Pass $index to ng-click on the md-list-item.

Markup

<div ng-controller="AppCtrl" ng-cloak="" ng-app="MyApp">
    <md-list-item class="md-3-line" ng-repeat="it in ec.events" ng-href="#/details" ng-click="showInfo($index)">
        <div class="md-list-item-text" layout="column" style="margin:5px 0;">
            <h3>{{it.description}}</h3>
            <p><span style="color:black">Chapter: </span>{{it.chapter}}</p>
            <p><span style="color:black">Date: </span>{{it.date}}</p>
            <p><span style="color:black">Type: </span>{{it.type}}</p>
            <p><span style="color:black">Days: </span>{{it.outing_days}}</p>
        </div>
        <md-divider></md-divider>
    </md-list-item>

    <br>
    Click info:
    <md-content flex layout-padding>
        <p>{{info.description}}</p>
        <p>{{info.chapter}}</p>
        <p>{{info.type}}</p>
       <p>{{info.date}}</p>
    <md-content>
</div>

JS

angular.module('MyApp',['ngMaterial', 'ngMessages'])

.controller('AppCtrl', function($scope) {
     $scope.ec = {};
     $scope.ec.events = [
         {description:'description1',date:'07/25/2016',type:'music',chapter:'Arizona'},
         {description:'description2',date:'08/15/2016',type:'movie',chapter:'Texas'}
     ];

     $scope.showInfo = function (index) {
         $scope.info = $scope.ec.events[index];
     }
});

Upvotes: 1

Related Questions