我想从ng-repeat指令中获取数组中的特定索引。
现在它打印出所有索引的信息......我只是一个打印第二个索引的信息,例如......
这是我的main.js:
app.controller('ProviderController', ['$http', '$scope', function($http, $scope) {
var provider = this;
//Get service from API
$http({
url: 'http://private-5d90c-kevinhiller.apiary-mock.com/angular_challenge/horror_movies',
method: "GET",
}).success(function(data) {
//process received Data with the processMovie function
var providers = processMovies(data);
//process Data with the calculatePercentage function
var percentages = calculatePercentage(providers, data.length);
var providerKeys = [];
for(key in providers) {
providerKeys.push(key);
}
$scope.providerKeys = providerKeys;
$scope.providers = providers;
$scope.percentages = percentages;
})
}]);
function processMovies(data) {
var providers = [],
movie,
offer;
var total = data.length;
for(var i = 0; i < data.length; i++) {
movie = data[i];
var movie_id = movie.id;
for(var j = 0; j < movie.offers.length; j++) {
offer = movie.offers[j];
//check if provider_id doesnt exist
if (typeof providers[offer.provider_id] === 'undefined') {
providers[offer.provider_id] = {};
providers[offer.provider_id][movie_id] = movie;
} else {
if(!providers[offer.provider_id].hasOwnProperty(movie_id)) {
providers[offer.provider_id][movie_id] = movie;
}
}
}
}
return providers;
}
我的Html:
<div ng-controller="ProviderController as provider">
<!-- Include provider controller from Angular-->
<div ng-repeat="providerId in providerKeys">
<ul>
<li>Provider {{providerId}}: {{percentages[providerId]|number :0 }}%
</li>
</ul>
</div>
<div ng-repeat="(provider_id, value) in providers track by $index">
<div ng-repeat="movie in value">
<p>{{movie.title}} Belongs to provider number: {{provider_id}}</p><br>
</div>
</div>
</div>
结果我得到了所有电影的所有电影..我怎么说Angular指令只是从提供者2那里得到的? 谢谢!
答案 0 :(得分:1)
您可以设置ng-show
或ng-if
来检查$index
。
<div ng-repeat="(provider_id, value) in providers track by $index" ng-if="$index==2">
<div ng-repeat="movie in value">
<p>{{movie.title}} Belongs to provider number: {{provider_id}}</p><br>
</div>
</div>