gpt4 book ai didi

javascript - 你如何将 ng-repeat 值推送到 Controller ?

转载 作者:行者123 更新时间:2023-11-30 08:37:44 28 4
gpt4 key购买 nike

我想使用 ng-repeat 和以下数组填充网页:

var array = [{name: Bill, age: 12, number: 1}, {name: Tyrone, age: 11, number: 2}, {name: Sarah, age: 14, number: 3}];

我希望能够按下一个按钮并向我的 Angular Controller 返回给定人员的姓名或号码值。我不知道该怎么做,尽管我尝试了按钮和输入尝试但都没有成功。您可以在下面的示例代码中看到其中一种尝试。

<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th colspan="1" class="text-center">
Name
</th>
<th colspan="1" class="text-center">
Age
</th>
<th colspan="1" class="text-center">
Number
</th>
</tr>
</thead>
<tbody filter-list="search"ng-repeat="a in array">
<tr>
<td class="col-xs-6 col-sm-6 col-md-6 col-xl-6">
{{a.name}}
</td>
<td class="col-xs-4 col-sm-4 col-md-4 col-xl-4">
<button ng-click="Age()" ng-model="a.age">This Age</button>
</td>
<td class="col-xs-2 col-sm-2 col-md-2 col-xl-2">
<input type="button" name="number" ng-click="Number()" ng-model="a.number" />
</td>
</tr>
</tbody>
</table>
</div>
</div>

我的 Controller 如下:

angular.module('startupApp')
.controller('ManageCardCtrl', function ($scope, $location, $http) {

var array = [{name: Bill, age: 12, number: 1}, {name: Tyrone, age: 11, number: 2}, {name: Sarah, age: 14, number: 3}];

$scope.Age = function(){
$http.post('api/age', {age: $scope.age})
.success(function (response) {
console.log(response)
})
.error(function (error) {
console.log(error)
});
}


$scope.number = function(){
$http.post('api/number', {number: $scope.number})
.success(function (response) {
console.log(response)
})
.error(function (error) {
console.log(error)
});
}
}

最佳答案

您可以在函数的参数中传递要使用的数据:

<button ng-click="Age(a.age)">This Age</button>

$scope.Age = function(age) {
$http.post('api/age', {age: age})
.success(function (response) {
console.log(response)
})
.error(function (error) {
console.log(error)
});
}

关于javascript - 你如何将 ng-repeat 值推送到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905972/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com