Shimul Chowdhury
Shimul Chowdhury

Reputation: 113

Angular js, pass value to other page

I have 2 pages.
Page:1 (index.php) form data, name, email, phone etc this data will be pass through angular JS

js page:

$scope.insert=function(){ $http.post("insert.php",{
       'name':$scope.name,
       'email':$scope.email}) 
    .success(function(datasuccess){
       $scope.name=datasuccess.name;
    $scope.email=datasuccess.email; 
    $scope.phone=datasuccess.phone; 
    $scope.cardDisplay();
    });
    };

page :1 (insert.php)

$datasuccess[]="Person Name";
    $datasuccess[]="Person email";
    $datasuccess[]="phone";
    print json_encode($datasuccess);

After success insert.php page jason data need to show to my index.php page

{{datasuccess.email}}
{{datasuccess.name}}

Ok in my index.php page have a form, after submit the data it will goes through angularJS in insert.php, after insert some data in insert.php page then from that page (insert.php) will return some data to index.php My angular code:

$scope.insert=function(){ $http.post("insert.php",{
   'name':$scope.name,
   'email':$scope.email}) 
.success(function(datasuccess){
   $scope.name=datasuccess.name;
$scope.email=datasuccess.email; 
$scope.phone=datasuccess.phone; 
$scope.cardDisplay();
});
};

Upvotes: 1

Views: 111

Answers (2)

Alon Eitan
Alon Eitan

Reputation: 12025

Create an associated array

$datasuccess = array(
    "name" => "Person Name",
    "email" => "Person email",
    "phone" => "phone"
);
echo json_encode($datasuccess);

And on the client side (JS) you'll get it as a JSON, with properties as reference from the server:

$scope.name = datasuccess.name;

Then you will be able to bind the information to the view simple by doing:

{{ name }}

Or:

<span ng-bind="name"></span>

Upvotes: 1

Rathma
Rathma

Reputation: 1313

I think you are not thinking in angular way, but one solution is to use $rootScope, simply inject this in your controller and share variables across your views, you can also create a service and assign those values to your service and use that service in your controllers.

controller('MainController', function($rootScope) {
      $rootScope.name = datasuccess.name;
});

and in your view you can simply access $rootScope as $root:

page index.html

<div>
    {{$root.name}}
</div>

page2.html

<div>
    {{$root.name}}
</div>

Upvotes: 0

Related Questions