duandiao3961
2016-03-03 17:55 阅读 187

如何使用Angular控制器将变量传递给另一个页面?

I have a HTLM table that contains information about persons ( id,name,..). I made the name act as a link to another page, so when I press the name of any person I want to pass the id of this person to another php page to work with it. Below is my code:

HTML:

<tr ng-repeat="x in data | filter:search:restrict">
    <td>{{ x.id }}</td>
    <td><button  ng-controller ="direct" class="btn btn-link" ng-click = "sendID(x.id)">{{ x.name }}</button></td>

controller :

fetch.controller('direct',function($scope,$http,$window){
        $scope.sendID=function(){
        $http.post("here.php",{'id':$scope.id})
                .success(function(headers,config){
                });
        $window.location.href = '/here.php'; 
    }

});

The second page, which I want to pass the id to:

<?php

$data = json_decode(file_get_contents("php://input"));

echo $data->id;

?>

When I click any button, the second page says:

"Notice: Trying to get property of non-object"

Any idea about what is wrong?!

Thank you.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanjuan3931 duanjuan3931 2016-03-03 18:53

    May be pass id through href:

    $window.location.href = '/here.php?id=' + $scope.id; 
    

    PHP

    <?php
      $id = $_GET['id'];
      echo $id;
    ?>
    
    点赞 评论 复制链接分享
  • dongyan7876 dongyan7876 2016-03-03 19:22

    if you just wanted it to be accessed in next page pass it as query string else save the value in $rootscope so that it can be accessed from any controller

    点赞 评论 复制链接分享

相关推荐