Kevin Hwang
Kevin Hwang

Reputation: 179

Angular Controller pass data to other controller when page changes

Hi I have a Java script file that controls all the controllers. I also have two html page that link one to another when the form is submitted, I need to pass a value to the other controller when the submit button is pressed.

I am able to log the value if I don't direct to another page, but once I direct to the other page, value becomes 'undefined'

here is my two angular controller

app.controller('createworkshopController', function($rootScope, $scope, $http) {

    //get bunch of stuff

    $scope.submitWorkshop = function() {    

      $http({method:'POST', data:$scope.result, url: 'url for post method'})
          .success(function(data) {
              $rootScope.ID = data.value;
              console.log($rootScope.ID); //this log return the value
          });
      };
});

But when I redirect to the other page the value become undefined here is the controller for second page

app.controller('showworkshopController', function($rootScope, $scope, $http) {

    console.log($rootScope.ID); //this becomes undefined

    $http.get('some_url?Id=' + $rootScope.ID)
    .success(function(data) {
        //do bunch of stuff
    });

    //do bunch of stuff
});

my first page html have a submit button written like this

    <a href="second-page.html" type="submit" class="btn btn-default" ng-click="submitWorkshop()">Submit</a>

Upvotes: 0

Views: 734

Answers (1)

ricick
ricick

Reputation: 5766

Your problem is your app will reinitialise on page refresh/redirect.

You need to build this as a one page app and not do the redirect, or store the value in cookies or local storage before the redirect, and the retrieve on loading the second page.

Basically angular is really meant for building applications, not as an add in to standard web forms.

Upvotes: 1

Related Questions