0

Hi i am displaying data using ng repeat.
I am displaying 3 persons in a template.. now when i click on 1 of them. I want to display in detail information of that person... so i need to make new controller and new template...but i know one way that is to pass one of the unique parameter like mobile no to the controller.. which then changes path with having mobile number to another controller and template.using location.path().search().. now in that 2nd controller i can request information again using 2nd parameter.. is this okay ??

I wanted to pass whole persons data from 1 controller to other but i don't know how can i do it...

Priya
  • 1,359
  • 6
  • 21
  • 41
Rohit
  • 17
  • 7

2 Answers2

2

Use a service to set the selected person's information and access the information using a routeParam for the user's unique id.

The code structure would look something like below

angular
  .module('app')
  .controller('ParentController', function (UserService) {
    
    $scope.onSelect = function (user) {
      UserService.setCurrentUser(user);
    }
  })
  .controller('ChildController', function ($routeParams, UserService) {
    
    $scope.user = UserService.getCurrentUser($routeParams.userId);
  })
  .factory('UserService', function () {
    var currentuser = {};
  
    return {
      getCurrentUser: getCurrentUser,
      setCurrentUser: setCurrentUser
    };
  
    function getCurrentUser () {
      return currentuser;
    }
  
    function setCurrentUser (user) {
      currentuser = user;
    }
  })
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
0

You can make the second controller child of the first controller.

All the data of the first controller can then be accessed in the second controller.

It also depends on the use cases, you can use the following methods to share data between controllers (if you do not want to do the above mentioned)

1) $localStorage 2) $sessionStorage 3) create a service

Rahul Arora
  • 4,503
  • 1
  • 16
  • 24