dongliang1893 2016-11-02 10:15
浏览 33
已采纳

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

After storing the value in PHP variable and trying to apply the condition check but I'm not able to do so.

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

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

Do you have a solution?

  • 写回答

1条回答 默认 最新

  • duanpao4172 2016-11-02 10:31
    关注

    try to understand below code..

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog