【发布时间】:2015-09-17 09:17:23
【问题描述】:
Angularjs 非常新。所以我有一些 JSON 文件正在读入我的网页,其中包含汽车对象的数组。我想要做的是让我的“按钮”在按下时提醒我该按钮特定的数据。
ng-repeat 运行了 8 次,所以这是数组的长度,但是在 angularJs 中,我不确定每次 ng-repeat 在我的按钮函数中传递时如何基本存储数组索引。
这是我的 .html 的 sn-p:
<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>
有问题的部分位于底部,我有一个保留“按钮”
我遗漏了我的 JSON 文件,在那里可以正常工作。我只是不确定如何在 ng-repeat 执行其操作时跟踪数组索引。
这是角度:
(function(){
var app = angular.module("myReserveApp", []);
app.controller("ButtonController", ["$scope", "$window", function($scope, $window){
$scope.buttonPress = function(){
$window.alert(JSON.stringify($scope.car[0]));
}
}]);
var MainController = function($scope, $http, $window){
var onGatherBoatData = function(response){
$scope.boat = response.data;
};
var onError = function(reason){
$scope.error = "Could not fetch Boat Data";
};
var onGatherCarData = function(response){
$scope.car = response.data;
};
var onError = function(reason){
$scope.error = "Could not fetch Car Data";
};
var onGatherTruckData = function(response){
$scope.truck = response.data;
};
var onError = function(reason){
$scope.error = "Could not fetch Truck Data";
};
$scope.message = "Hello, Angular Here!";
};
app.controller("MainController", ["$scope", "$http", "$window", MainController]);
}());
目前在代码的顶部,我只是让它提醒对象 [0],但我希望它特定于按下哪个按钮。任何帮助表示赞赏,谢谢。
【问题讨论】:
-
您可以在 HTML 中使用
buttonPress($index)来传递索引。 -
只是一个简短的评论:通过在 Stackoverflow 上快速搜索或咨询 AngularJS documentation,您可以轻松找到问题的答案。
标签: javascript jquery arrays json angularjs