Javascript 如何在angularJS中跟踪数组索引?
对Angularjs来说非常陌生。所以我有一些JSON文件,我正在阅读到我的网页,其中包含和数组的对象是汽车。我想做的是,当按下“按钮”时,提醒我该按钮特定的数据 ng repeat运行了8次,这就是数组的长度,但在angularJs中,我不确定每次ng repeat通过我的按钮函数时,基本上如何存储数组索引 这是my.html的一个片段:Javascript 如何在angularJS中跟踪数组索引?,javascript,jquery,arrays,json,angularjs,Javascript,Jquery,Arrays,Json,Angularjs,对Angularjs来说非常陌生。所以我有一些JSON文件,我正在阅读到我的网页,其中包含和数组的对象是汽车。我想做的是,当按下“按钮”时,提醒我该按钮特定的数据 ng repeat运行了8次,这就是数组的长度,但在angularJs中,我不确定每次ng repeat通过我的按钮函数时,基本上如何存储数组索引 这是my.html的一个片段: <div class="carTable table-responsive text-center" ng-controller="Butto
<div class="carTable table-responsive text-center" ng-controller="ButtonController" >
<table class="table specTable">
<thead>
<tr>
<th>Make</th>
<th>Model</th>
<th>Year</th>
<th>Color</th>
<th>Milage</th>
<th>Doors</th>
<th class="reserve">Horsepower</th>
<th>Price</th>
<th class="reserve"></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="cars in car | orderBy:'year'">
<td>{{cars.year}}</td>
<td>{{cars.model}}</td>
<td>{{cars.make}}</td>
<td>{{cars.color}}</td>
<td>{{cars.mileage | number}}</td>
<td>{{cars.doors}}</td>
<td>{{cars.horsepower}}</td>
<td>{{cars.price | number}}</td>
<td><div class="panel panel-default"><a href="" ng-click="buttonPress()">Reserve</a></div></td>
</tr>
</tbody>
</table>
</div>
目前,在代码的顶部,我只是有一个提醒对象[0],但我希望它是特定于按下哪个按钮。非常感谢您的帮助。
$index
指的是ng repeat中的索引。因此,如果要在单击按钮时将数组中的索引传递给函数,请将buttonPress()
更改为buttonPress($index)
您必须将控制器更改为如下所示:
$scope.buttonPress = function(index){
$window.alert(JSON.stringify($scope.car[index]));
}
$index
指的是ng repeat中的索引。因此,如果要在单击按钮时将数组中的索引传递给函数,请将buttonPress()
更改为buttonPress($index)
您必须将控制器更改为如下所示:
$scope.buttonPress = function(index){
$window.alert(JSON.stringify($scope.car[index]));
}
$index
指的是ng repeat中的索引。因此,如果要在单击按钮时将数组中的索引传递给函数,请将buttonPress()
更改为buttonPress($index)
您必须将控制器更改为如下所示:
$scope.buttonPress = function(index){
$window.alert(JSON.stringify($scope.car[index]));
}
$index
指的是ng repeat中的索引。因此,如果要在单击按钮时将数组中的索引传递给函数,请将buttonPress()
更改为buttonPress($index)
您必须将控制器更改为如下所示:
$scope.buttonPress = function(index){
$window.alert(JSON.stringify($scope.car[index]));
}
要执行以下操作,只需在
ngRepeat
中传递当前数据即可。此外,如果需要当前索引,ngRepeat
指令提供特殊属性,如作为迭代器的$index
$scope.buttonPress = function(car, index){
//Retrieve current data of the ngRepeat loop
console.log(car);
//Current index of your data into the array
console.log(index);
}
然后您可以这样调用您的函数:
<a href="" ng-click="buttonPress(cars, $index)">Reserve</a>
要执行以下操作,只需在
ngRepeat
中传递当前数据即可。此外,如果需要当前索引,ngRepeat
指令提供特殊属性,如作为迭代器的$index
$scope.buttonPress = function(car, index){
//Retrieve current data of the ngRepeat loop
console.log(car);
//Current index of your data into the array
console.log(index);
}
然后您可以这样调用您的函数:
<a href="" ng-click="buttonPress(cars, $index)">Reserve</a>
要执行以下操作,只需在
ngRepeat
中传递当前数据即可。此外,如果需要当前索引,ngRepeat
指令提供特殊属性,如作为迭代器的$index
$scope.buttonPress = function(car, index){
//Retrieve current data of the ngRepeat loop
console.log(car);
//Current index of your data into the array
console.log(index);
}
然后您可以这样调用您的函数:
<a href="" ng-click="buttonPress(cars, $index)">Reserve</a>
要执行以下操作,只需在
ngRepeat
中传递当前数据即可。此外,如果需要当前索引,ngRepeat
指令提供特殊属性,如作为迭代器的$index
$scope.buttonPress = function(car, index){
//Retrieve current data of the ngRepeat loop
console.log(car);
//Current index of your data into the array
console.log(index);
}
然后您可以这样调用您的函数:
<a href="" ng-click="buttonPress(cars, $index)">Reserve</a>
首先,感谢你们两位的快速回复。这两个答案都有效。在阅读你的帖子之前,我找到了另一种方法
<div class="panel panel-default">
<a href="" ng-click="buttonPress(car.indexOf(cars))">Reserve:{{car.indexOf(cars)}}</a>
</div>
现在,当我点击“按钮”时,它会将数组索引发回给我,所以现在我应该能够处理这些数据了。再次感谢你们两位的帮助。首先,感谢你们两位的快速回复。这两个答案都有效。在阅读你的帖子之前,我找到了另一种方法
<div class="panel panel-default">
<a href="" ng-click="buttonPress(car.indexOf(cars))">Reserve:{{car.indexOf(cars)}}</a>
</div>
现在,当我点击“按钮”时,它会将数组索引发回给我,所以现在我应该能够处理这些数据了。再次感谢你们两位的帮助。首先,感谢你们两位的快速回复。这两个答案都有效。在阅读你的帖子之前,我找到了另一种方法
<div class="panel panel-default">
<a href="" ng-click="buttonPress(car.indexOf(cars))">Reserve:{{car.indexOf(cars)}}</a>
</div>
现在,当我点击“按钮”时,它会将数组索引发回给我,所以现在我应该能够处理这些数据了。再次感谢你们两位的帮助。首先,感谢你们两位的快速回复。这两个答案都有效。在阅读你的帖子之前,我找到了另一种方法
<div class="panel panel-default">
<a href="" ng-click="buttonPress(car.indexOf(cars))">Reserve:{{car.indexOf(cars)}}</a>
</div>
现在,当我点击“按钮”时,它会将数组索引发回给我,所以现在我应该能够处理这些数据了。再次感谢你们两位的帮助。您可以在HTML中执行
按钮按下($index)
来传递索引。只需一句简短的评论:您可以通过在Stackoverflow上进行快速搜索或咨询AngularJS轻松找到问题的答案。您可以执行按钮按下($index)
在您的HTML中传递索引。只需一句简短的评论:您可以通过在Stackoverflow上进行快速搜索或咨询AngularJS轻松找到问题的答案。您可以按按钮($index)
在您的HTML中传递索引。只需一句简短的评论:您可以通过在Stackoverflow上进行快速搜索或咨询AngularJS轻松找到问题的答案。您可以按按钮($index)
在您的HTML中传递索引。只需一句简短的评论:您可以通过在Stackoverflow上进行快速搜索或咨询AngularJS轻松找到问题的答案。