dongtong0796 2014-01-14 00:32
浏览 77
已采纳

angularjs为隐藏字段提供'undefined',将php会话变量作为值

I have found a lot or resources on how to pass a hidden input field value to an angularjs script, but not if that value is being read from a php session.

This is my html code:

    <div ng-controller="ImgCtrl">
      <input type="hidden" ng-model="uid" value="<?=$_SESSION["uid"];?>" >
      <input type="file" ng-file-select="onFileSelect($files)" >
      <br/>
      <span>{{ message }}</span>
    </div>

I have also tried it as this:

    <input type="hidden" ng-model="uid" value="{{<?=$_SESSION["uid"];?>}}" >

EDIT The angularjs code that will handle it is:

var ImgCtrl = [ '$scope', '$upload', function($scope, $upload) {
  $scope.onFileSelect = function($files) {
    for (var i = 0; i < $files.length; i++) {
      var file = $files[i];
      $scope.upload = $upload.upload({
        url: 'image_upload.php', 
        method: 'POST',
        data: {
            'uid': $scope.uid
        },
        file: file
      }).success(function(data, status, headers, config) {
        $scope.message = data;
      }).error(function(data, status) {
         alert(data);
      });
    }
  };
}];

Which was one of the answers I found, but I can not seem to find how to read a php session variable in angularjs.

Anyone have an idea?

Thanks!

  • 写回答

2条回答 默认 最新

  • douzheren3349 2014-01-14 00:51
    关注

    The first and easiest way to do what you are looking for is to basically create a global javascript variable and set the value in your PHP code. This will then get passed on to the controller. In the php page you would have:

    <script>var serverVariable=<?=$_SESSION["uid"];?>;</script>
    

    And in your controller you would have the line that says:

    $scope.userId = serverVariable;
    

    After that you can use the userId from scope wherever you would like and it will work! There is a plunker demonstrating this here: http://plnkr.co/edit/7xEGM0Rz80TJQc0kPBwz?p=preview.

    Notes

    So, of course the easiest thing to do isn't always the best thing to do. First, if you are passing user id around client side - whether its in a hidden variable or if its in a javascript global the client can manipulate your value. You don't want to rely on whatever comes back from the client in your "post" or "put" then - but rather you should use the value that you have stored in session. This is a really good rule of thumb across the board for any sensitive values.

    Another thing - this is the easiest but it is far from the only option. You might consider using the "ng-init" directive on the element where your controller is defined. You can find a stack question with that answer here: Can you pass parameters to an AngularJS controller on creation?. Again, simply pass the values in on the server side into your init routine and everything should get passed to the controller correctly!

    I hope that my answer at least has set you on the right track. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元