我有一个 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;
});
}
});
如何将 $scope.product_code 从 ng-click 传递到 Controller ?
<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>
最佳答案
只需将 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>
关于javascript - 使用 ng-click 获取示波器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583267/