2015-06-01 42 views
1

我有一个php api更新我的数据库,但我想控制哪个项目用ng-click更新。用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; 
     }); 

     } 

    }); 

如何将$ scope.product_code从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> 
+1

这应该是可行的..这个[也许是一个快速审查http://stackoverflow.com/questions/17039926/adding-参数到NG-点击功能里面-NG-重复,好像而不是到工作(http://stackoverflow.com/questions/17039926/adding-parameter-to-ng-click-function-inside似乎不工作)可能会有所帮助 - 例如,使用ng-click函数调用将ng-repeat中的数据传递给控制器​​中的函数(控制器函数当然也应该期待一些) –

+0

这个解决方案的工作非常完美。谢谢 –

+0

真棒 - 当你开发这个应用程序,它可能是一个很好的做法,以提取'$ http'的东西到'服务'=) –

回答

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>