0

I have an ng-repeat set up showing a list of groups on a page. I would like to click on one of those groups and have the data specific to the clicked group show up on a 'details' page i have already created. I have tried several methods to do this and the farthest I have gotten is not very impressive. Any help would be appreciated.

index.html:

 <div class="containerList">
    <div class="row">
      <div class="col-md-4" ng-repeat="group in data.groups | filter: filterObject | filter: search">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h5 class="panel-title"><a href="#groupDetail" ng-click="open(group)">{{group.name}}</a></h5>
          </div>
          <div class="panel-body">
            <div class="btn-group">
              <button type="button" class="btn btn-default" >{{group.products}}</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

groupCtrl:

creativeBillingApp.controller('GroupCtrl', ['$scope', 'groupsService', function( $scope, groupsService, $firebase ) {

$scope.newGroup = {
 name: '',
 status: ''
};

$scope.data = {};
$scope.data.groups = groupsService.getGroups();

$scope.groups = groupsService;

$scope.addGroup = function(newGroup) {

groupsService.addGroup(newGroup);

$scope.newGroup = {
    name: '',
    status: ''
};

};
 $scope.updateGroup = function (id) {
   groupsService.updateGroup(id);
 };

$scope.removeGroup = function(id) {
groupsService.removeGroup(id);
};


$stateProvider
 .state('groupdetail', {
    url                 : '/groupdetail/:id',
    controller          : 'GroupCtrl',
    templateUrl         : 'groupDetails.html'
 })


  $scope.groupId = $stateParams.id; // You will get id, and you can extract data using this id

}])

 .factory('groupsService', ['$firebase', 'FIREBASE_URI',
   function ($firebase, FIREBASE_URI) {
    var ref = new Firebase(FIREBASE_URI + '/groups');

   var groups = $firebase(ref).$asArray();

var getGroups = function(){
  return groups;
};

var addGroup = function (newGroup) {
  console.log(newGroup)
  groups.$add(newGroup);
};

var updateGroup = function (id){
  groups.$save(id);
};

var removeGroup = function (id) {
  groups.$remove(id);
};

return {
  getGroups: getGroups,
  addGroup: addGroup,
  updateGroup: updateGroup,
  removeGroup: removeGroup,
}

}]);

groupDetail.html:

   <div class="panel panel-default">
    <div class="panel-heading">
      <h5 class="panel-title">
         {{group.name}}
      </h5>
    </div>
   </div>
3
  • 1
    You want to navigate into a new page? can you console.log(group) inside open to check the handler is working? Commented Dec 11, 2014 at 15:23
  • 1
    Are you trying to move to a new view with details or do you want to open a modal window inside your current view? If the 1st case, then dont use no $scope.open, just navigate to the new view (via a href) and add the info about which group was clicked as url parameter. Commented Dec 11, 2014 at 15:37
  • I added some more detail to the controller and made some changes/additions based on the answer below. I am trying to open in a new page but am having trouble pulling the data from firebase in. Commented Dec 11, 2014 at 16:06

1 Answer 1

1

You can simply use stateProivder

angular.module('myapp', ['ui.router'])
.config(function ($stateProvider){
    $stateProvider
        .state('groupdetails', {
            url                 : '/groupdetails/:id',
            controller          : 'GroupDetailsCtrl',
            templateUrl         : 'groupDetails.html'
        })
})
.controller("GroupDetailsCtrl", function($firebase, $stateProvider) {
    // now since you are using firebase you need to call service which returns data by id
    firebaseService.getGroup($stateProvider.id).success(function(group) {
        $scope.group = group;
    })
})

I am assuming that you have defined firebase in your firebaseService (Angular Service) and defined methods there to get, save, remove data.

Now in your main html you need to link data-ui-sref

<div class="containerList">
<div class="row">
  <div class="col-md-4" ng-repeat="group in data.groups | filter: filterObject | filter: search">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h5 class="panel-title"><a href="#groupDetail" data-ui-sref="groupdetails({ id: group.id })">{{group.name}}</a></h5>
      </div>
      <div class="panel-body">
        <div class="btn-group">
          <button type="button" class="btn btn-default" >{{group.products}}</button>
        </div>
      </div>
    </div>
  </div>
</div>

So as soon as you click on this link, you will be redirected to a screen with url #/groupdetails/123

Now in your GroupDetailsCtrl, you can extract the data from your groups

$scope.groupId = $stateParams.id; // You will get id, and you can extract data using this id

Make sure you have injected ui.router

Hope this helps :)

Sign up to request clarification or add additional context in comments.

3 Comments

I added more detail to my controller. Could you take another look, please?
thank you for replying. Is it necessary I use ui.router I am cuurently just using ngroute and routeProvider... I am also getting this error in the command line: TypeError: Cannot read property 'id' of undefined
If you are using ngRouter then, you just define the routeProvider. Something like this, $routeProvider. when('/groupdetails/:id', { templateUrl: 'groupDetails.html', controller: 'GroupDetailsCtrl' }) and in your controller you need to get this id by $routeParams.id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.