0

I have a HTML page for listing users from MongoDB , this page delete and update users , i have a problem in the update button i want when i click on the button a new Html page appears with a form and get the id of the user from the previous view in another controller for the new controller.. i passed the id in ng-click(item._id) and i can show it in the console but in the new page the new controller can't read the id ( id is not defined )..

$scope.update = function (id) {
        console.log(id);
        $http.get('/readbyid/' + id).success(function (messages) {
            $scope.item = messages;
        });

}

in the new controller :

$http.get('/readbyid/' + id).success(function (messages) {
        $scope.Candidator = messages;
        console.log(id);
    });
sidgate
  • 14,650
  • 11
  • 68
  • 119
  • if you are using route then you have to pass id in routeparams or keep selected user id in service and get it there in next page – Jayant Patil Mar 22 '17 at 08:07
  • You can send that id in URL either with express or Angular(SPA case) – Ashutosh Jha Mar 22 '17 at 08:09
  • Possible duplicate of [Share data between AngularJS controllers](http://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers) – Mistalis Mar 22 '17 at 08:16

2 Answers2

3

Define route for current and another view :

.state({
  name:'currentView',
  url:'/currentView',
  templateUrl:'views/currentView.html',
  controller:'currentViewCtrl'
})
.state({
  name:'updateView/:customerID',
  url:'/updateView/:customerID',
  templateUrl:'views/updateView.html',
  controller:'updateViewCtrl'
})

Call a function on button click to send customerID to next view with

$location.path('updateView/' + customerID);

On next view access customerID with

$stateParams['customerID']

Well this is just to give you an idea, I guess you can connect the dots now. Tell me if i'm not clear enough.

Chirag
  • 179
  • 3
  • 16
  • @MarouenAyadi Could you **accept** the answer if it solved your issue? (click the mark at the left of the answer) – Mistalis May 03 '17 at 08:52
1

There some way to achieve the result depending on your setup.

  1. If you are using Angular UI router, you have a $stateParams service which stores data during routeChanges and brings data from a view to another
  2. If you're using "vanilla Angular" you can create a storage service relative to the module in which that transition happens. This way you have to take care of cleaning what you don't need everytime. A sort of pain in the neck, in my opinion. Angular UI is a better choice
  3. You can use query string parameters, if data can live in your history
Phugo
  • 400
  • 1
  • 10