Home > Mobile >  How to pass variables from one page to another with AngularJS?
How to pass variables from one page to another with AngularJS?

Time:05-05

I'm working with 2 different controllers (and js files) for my index.html (controllerIndex on index.js) and pageA.html (controllerPageA on pageA.js).

index.html has 2 links that lead to the same page, but need to pass a parameter with different values depending on which one I click in order to use them for an API on controller2.

How can I do this?

index.html:

<!-- Should pass ParameterA here -->
<a ng-model="parameter" ng-init="parameter = 'parameterA'" href="pageA.html">Page A1</a> 
<!-- Should pass ParameterB here -->
<a ng-model="parameter" ng-init="parameter = 'parameterB'" href="pageA.html">Page A2</a>

pageA.js:

$http({
  method: 'GET',
  url: 'localhost:8888/ProjectName/API/items/list?type='   parameter //where parameter = 'parameterA' or 'parameterB' depending on which link was clicked
}).then(function successCallback(response) {
   $scope.items = response.data;
  }, function errorCallback(response) {
    alert("Error");
  }); 

Thanks in advance!

CodePudding user response:

I'm not familiar with Angular. But it looks to me that you can transfer your parameters simply via GET-parameters:

<a ng-model="parameter" ng-init="parameter = 'parameterA'" href="pageA.html?parameter=parameterA">Page A1</a> 

and retreive them like this on the pageA.html:

   var query = window.location.search.substring(1);
   var parts = query.split("=");
   var parameter = parts[1];
  • Related