Jared Whipple
Jared Whipple

Reputation: 1171

Get scope data with ng-click

I have a php api which updates my database, but I would like to control which item is updated with 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;
      });

      }

    });

How would I pass $scope.product_code to the controller from ng-click?

<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>

Upvotes: 2

Views: 1829

Answers (1)

Lucas L Roselli
Lucas L Roselli

Reputation: 2830

Just pass your ngRepeat item as a parameter.

Try this:

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;
  });

  }

});

your 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>

Upvotes: 1

Related Questions