2015-06-01 99 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>