我有返回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}} ..
你有沒有想過ajax – Gowri 2014-09-02 06:35:34
我不知道我該怎麼做,如果你有請求告訴我,我是angularJs的新手,我不知道如何在angularJs中使用php代碼。 ..我不知道我必須編寫seprate控制器採取php代碼或我可以直接使用... – 2014-09-02 06:39:03
您不能使用客戶端代碼內的服務器端代碼。但是,您的客戶端可以使用ajax調用來調用資源,並且該資源可以包含使用php呈現的文檔。 – sensorario 2014-09-02 06:50:39