dsfdsf48652 2016-12-28 07:30
浏览 61
已采纳

在HTML中的php foreach里面的Angular Javascript

I am trying to make a simple shopping website using mainly PHP and HTML. Inside PHP, I created objects from a class, and those objects are in array. Now inside HTML, I am printing each object's photo and name using PHP's foreach. Since this is a shopping website, I have added a FORM under each item photo where the customer can type in the number of the items they wish to purchase. The default value is 0. Now, using angularJS, What I want to do is: 1)when the value(the number of the items they want) is 0, nothing happens and the webpage prints nothing. 2)when the value is more than 0, print the number * the price right next to the form, so the user can see in real time how much the total is going to be for that specific item.

Here is my code..

<?php
class Stock{
    public $name; //in real program it is private. 
    public $price;
}

//Create objects and initialize them by giving each of them name and price
//Assume there are 3 objects
$items = array($object1, $object2, $object3);
?>

//now in HTML code
<form method="POST" action="order.php">
    <?php foreach($items as $item): ?>
        <!--Print name and price of each -->
    <input type="text" value="0" name="<?php echo $item->name; ?>"> {{ <!--*****--> }}
<?php endforeach ?>

I want to print the product of the number * price of the item right next to the form only if the value is greater than 0. I tried adding ng-model="quantity" inside input tag and wrote {{ quantity * $_POST[$item->name] }}. However, this does not fully work. This does print the product, but because it is inside foreach, when I change the value in the form for an item, the values for the other items' form change along with it. How can you apply angularJS only for the specific item?

  • 写回答

2条回答 默认 最新

  • dongren9966 2017-05-03 08:09
    关注

    Your loop must be in angular to do this.

    So you could bind it as a model ..

    FORM.PHP

    <div ng-app="appModule">
    <form action="order.php" method="post" ng-controller="orderFormController">
        <div ng-repeat="item in itemList">
            {{ item.name }} (price - {{ item.price }}): <input type="text" value="0" name="{{item.name}}" ng-model="qty[item.name]" />
            Total: <span ng-if="qty[item.name] > 0">{{ item.price*qty[item.name] }}</span>
        </div>
    </form>
    </div>
    

    MODULE.JS

    var app = angular.module("appModule", []); 
    app.controller("orderFormController", function($scope) {
        // $scope.itemList - ajax your item list here
        $scope.itemList = [
            {name: 'Pencil', 'price': 10},
            {name: 'Paper', 'price': 2},
            {name: 'Folder', 'price': 13},
        ];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?