0

i try sample project which take some data in form in html file .. then pass it to spring service .. which return object successfully .. now i want to pass this object to another Html fie to Display

Form's Html File :

<!DOCTYPE html>
<html ng-app="phase2">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<head>
<title>Sign UP Page</title>
<script src="RigesterationController.js"></script>

</head>
<body >

<center>

<p>Enter Your User Name : <input type="text" , name="UserName" id ="UName"  required /> </p>
<p>Enter Your Email : <input type="text" , name="Email" id ="email" required /> </p>
<p>Enter Your Password : <input type="password" , name="pass" id ="Pass" required/> </p>
<p>Choose Gender : <br> Male<input type="radio" name="gender" value="m" id="Gender" />    Female<input type="radio" name="gender" value="f" id="Gender"/> </p>
<p>Choose User Type :<br>  Student<input type="radio" name="UserType" value="s" id="Utype" />     Teacher<input type="radio" name="UserType" value="t" id="Utype"/> </p>

<div ng-controller="SignUP">
<input type="button" name="signup" value="SignUP" ng-click="save()" />
</div>

</center>
</body>
</html>

RigesterationController.js file :

angular.module("phase2" , [])

.controller("SignUP" , function($scope , $http )
{
 var dat ;
 $scope.save = function() {
  var email= document.getElementById("email").value;
  var UName=document.getElementById("UName").value;
  var Pass=document.getElementById("Pass").value;
  var gender=document.getElementById("Gender").value;
  var UserType=document.getElementById("Utype").value;
  var Info ;
  $http.get('http://localhost:8090/SignUp/'+email+'/'+UName+'/'+Pass+'/'+gender+'/'+UserType)
  .then(function(response)
   {
    Info = response.data;
    dat=Info ;
    alert(dat.name) ;
    window.location.href="http://localhost:8060/TheAngular_Project/StudentPage.html";
   });
  
  }
 
});

second Html file :

<!DOCTYPE html>
<html ng-app="phase2">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="RigesterationController.js"></script>
<head>
<title>Student Page</title>

</head>

<body>
<div ng-controller="SignUP">
<p><span class="name">Welcome  {{dat.name}}</p>
</div>
</body>
</html>

now nothing appeared in dat.name in second html file .. although .. in regestrationController.js ..I test dat.name in an allert and it appeared successfuly .. thanks in advance

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Ahmed Hassan
  • 65
  • 1
  • 2
  • 12

1 Answers1

0

i found the answer by Wawy in this post.. [AngularJS - Passing data between pages

You need to create a service to be able to share data between controllers.

app.factory('myService', function() {
 var savedData = {}
 function set(data) {
   savedData = data;
 }
 function get() {
  return savedData;
 }

 return {
  set: set,
  get: get
 }

});

In your controller A:

myService.set(yourSharedData);

In your controller B:

$scope.desiredLocation = myService.get();

Remember to inject myService in the controllers by passing it as a parameter.

Community
  • 1
  • 1
Nitin Walia
  • 423
  • 2
  • 9