通过ng-click获取范围数据

时间:2015-06-01 21:05:19

标签: javascript php angularjs

我有一个更新我的数据库的php api,但我想用ng-click控制更新哪个项目。

    app.controller('ReviewProductsController', function ($scope, $http) {

      $scope.hide_product = function () {

        $scope.message = "";

            var request = $http({
              method: "post",
              url: "/data/hideProduct.php",
              data: {
                product_code: $scope.product_code
              },
              headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            });

      /* Check whether the HTTP Request is Successfull or not. */
      request.success(function (data) {
        $scope.message = "Product Hidden: "+data;
      });

      }

    });

如何通过ng-click将$ scope.product_code传递给控制器​​?

<tr ng-repeat="product in productsList.Items">
  <td>{{ product.product_code.S }}</td>
  ...
  <td>
    <button ng-click="hide_product()" type="button">Hide</button>
  </td>
</tr>

1 个答案:

答案 0 :(得分:1)

只需将您的ngRepeat项目作为参数传递。

试试这个:

app.controller('ReviewProductsController', function ($scope, $http) {

  $scope.hide_product = function (code) {

    $scope.message = "";

        var request = $http({
          method: "post",
          url: "/data/hideProduct.php",
          data: {
            product_code: code
          },
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });

  /* Check whether the HTTP Request is Successfull or not. */
  request.success(function (data) {
    $scope.message = "Product Hidden: "+data;
  });

  }

});

你的HTML:

<tr ng-repeat="product in productsList.Items">
  <td>{{ product.product_code.S }}</td>

  <td>
    <button ng-click="hide_product(product.product_code)" type="button">Hide</button>
  </td>
 </tr>