如何将AngularJS变量的值赋给PHP变量

时间:2016-11-02 10:15:26

标签: php angularjs

将值存储在PHP变量中并尝试应用条件检查 但是我无法这样做。

<div ng-repeat="item in items track by $index">      
<?php
    $index= '{{$index}}';
    var_dump($index);

    if ($index==="1" || $index=="1") {
        echo $index;
        # code...
    }
?>
</div>

你有解决方案吗?

1 个答案:

答案 0 :(得分:0)

尝试理解以下代码..

<强> db.php中

<?php

include '../includes/config.php';

switch ($_GET['action']) {
    case 'add_product' :
        add_product();
        break;

    case 'get_product' :
        get_product();
        break;
}
function get_product() {
    $qry = mysql_query('SELECT * from product');
    $data = array();
    while ($rows = mysql_fetch_array($qry)) {
        $data[] = array(
            "ng_id" => $rows['ID'],
            "ng_name" => $rows['Name'],
            "ng_desc" => $rows['Detail'],
            "ng_price" => $rows['Price'],
            "ng_quantity" => $rows['Quantity']
        );
    }
    print_r(json_encode($data));
    return json_encode($data);
}

<强> app.js

var app = angular.module('store', ["ngRoute"]);
app.controller("mainController", function ($scope, $http) {
    $scope.get_product = function () {
        $http.get("db.php?action=get_product").success(function (data) {

            $scope.myData = data;
        }).error(function () {
            $scope.data = "error in fetching data";
        });
    };
}

<强>的index.php

//if you want to use bootstrap div structure
    <div class="row" ng-repeat="x in myData">
      <div class="col-md-4">{{x.ng_id}}</div>
      <div class="col-md-4">{{x.ng_name}}</div>
      <div class="col-md-4">{{x.ng_desc}}</div>
    </div>

//if you want to use table structure
    <table>
    <tbody data-ng-init="get_product();">
                   <tr data-ng-repeat="x in myData ">
                      <td>{{$index}}</td>
                      <td>{{ x.ng_name}}</td>
                      <td>{{ x.ng_desc}}</td>
                      <td>{{ x.ng_price}}</td>
                      <td>{{ x.ng_quantity}}</td>
                      <td>
                         <a href="" data-ng-click="edit_product(x.ng_id)">Edit</a> | 
                         <a href="" data-ng-click="delete_product(x.ng_id)" modal-backdrop="static">Delete</a>
                      </td>
                   </tr>
                </tbody>
    </table>