2014-09-02 31 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