Kullanım ng-repeat
$index
adını alır. modeliniz üzerinde döngü Yapma, daha basittir bkz:
<p>Hello {{name}}!</p>
<div class="dropdown category" style="position:relative">
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
<ul class="dropdown-menu">
<li ng-repeat="objName in test" ng-click="getName($index)">
<a href=""> {{objName.name}}</a>
</li>
</ul>
</div>
{{ selectedCategory }}
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {
$scope.name = 'Reinforcement';
$http.get('test.json')
.success(function (data) {
$scope.test = data;
});
$scope.getName = function (index) {
console.info($scope.test[index].name);
$scope.selectedCategory = $scope.test[index].name;
}
});
NOT/tavsiye: Üst kapsam erişim sağlar $parent
ile karmaşık hale getirebileceğini beri parent
gibi model adını kullanmaktan kaçının.
İkinci durumda
, sen ebeveyn $ indeksi almak zorunda $parent.$index
(beri iç içe ng-reapeat) ama aynı hile = array modeli üzerinde hiçbir döngü:
html:
<ul class="dropdown-menu">
<li class="dropdown" ng-repeat="entity in entitis" >
<a class="trigger right-caret">{{ entity }}</a>
<ul ng-if="entity | filter : 'Main'" class="dropdown-menu sub-menu">
<li ng-repeat="domain in ast" ng-click="getDomain($parent.$index, $index)">
<a href=""> {{domain.name}}</a>
</li>
</ul>
</li>
</ul>
</div>
{{ items }}
<br/>
<br/>
<p><b>parent index : {{astParentIndex}}</b></p>
<p><b>index : {{astIndex}}</b></p>
js
$scope.getDomain = function (parentIndex, index) {
$scope.astParentIndex = parentIndex;
$scope.astIndex = index;
$scope.items = $scope.ast[parentIndex].children;
}
. Mücadele ettiğin problem nedir? – SayusiAndo
@SayusiAndo "Tekel" e tıklarsanız, "Dünya" yı döndürür, bu benim mücadelem – Anton
neden -1? Bu soru açısal başlayanlar için iyidir. – MacKentoch