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);
    });
3
  • 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 Commented Mar 22, 2017 at 8:07
  • You can send that id in URL either with express or Angular(SPA case) Commented Mar 22, 2017 at 8:09
  • Possible duplicate of Share data between AngularJS controllers Commented Mar 22, 2017 at 8:16

2 Answers 2

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.

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

1 Comment

@MarouenAyadi Could you accept the answer if it solved your issue? (click the mark at the left of the answer)
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

1 Comment

Ok, use url params is a better way

Your Answer

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