2014-09-02 91 views
2

我有返回json响应的代码,但我不知道如何在php函数中传递该值。如何将angularjs数组值作为变量传递给php函数?

代码:

<div class="container" ng-controller="askbyctrl" > 
      <div class="row" ng-repeat="q in qa.data" > 
      <div class="col-md-12 col-xs-12 col.sm.12" > 
       <div class="artist-data pull-left" style = "background-color:#BEB7B7;"> 
        <div class="artst-pic pull-left"> 
         <?php 
          function cache_image($id){ 
           //replace with cache directory 
           $image_path = 'C:\xampp\htdocs\QA_UI\images'; 

           $image_url = 'http://localhost:8000/api/image/' + id; 

           //get the name of the file 
           $exploded_image_url = explode("/",$image_url); 
           $image_filename = 'id.jpg'; 
           $exploded_image_filename = explode(".",$image_filename); 
           $extension = end($exploded_image_filename); 
           //make sure its an image 
           if($extension=="jpg") { 
            //get the remote image 
            $image_to_fetch = file_get_contents($image_url); 
            if($image_to_fetch == '{"error":true,"details":"No image."}') { 
             echo 'file not found'; 
            } else { 
             //save it 
             $local_image_file = fopen($image_path.'/'.$image_filename, 'w+'); 
             chmod($image_path.'/'.$image_filename,0755); 
             fwrite($local_image_file, $image_to_fetch); 
             fclose($local_image_file); 
             echo 'copied'; 
            } 
           } 
          //} 
          ?> 
         <img ng-src="images/{{q.userID}}.jpg" alt="" class="img-responsive" /> 
        </div> 

请参阅PHP函数的代码我怎样才能通过{{q.userID}} ..

+0

你有没有想过ajax – Gowri 2014-09-02 06:35:34

+0

我不知道我该怎么做,如果你有请求告诉我,我是angularJs的新手,我不知道如何在angularJs中使用php代码。 ..我不知道我必须编写seprate控制器采取php代码或我可以直接使用... – 2014-09-02 06:39:03

+1

您不能使用客户端代码内的服务器端代码。但是,您的客户端可以使用ajax调用来调用资源,并且该资源可以包含使用php呈现的文档。 – sensorario 2014-09-02 06:50:39

回答

2

你不能这样做的。 PHP代码在服务器端运行,客户端在浏览器中运行角码。也就是说,当服务器返回编译好的html到浏览器时,它已经完成了该文件中的所有php代码。

如果你想检查图像是否存在,然后缓存它,你可以从角度做到这一点,例如使用$ http。用$ http调用你的后端脚本,它可以做你想做的事情,然后在成功与否的情况下返回。

+0

那么,如何将该对象作为变量传递给该代码,因为我必须通过userID并将该图像复制到我的文件夹中.....这个任务是通过php代码,我有帖子.....请看,并告诉我该怎么做... – 2014-09-02 06:53:37

+0

你不能在ng-repeat中做到这一点。如果你从服务器端得到这个qa.data集合,你可以在返回角度控制器之前遍历该列表,然后调用该函数。 – Mikko 2014-09-02 07:05:13

相关问题