我正在尝试使用指令“滑块”的父级 ID 来遍历数组范围 [ID],但该 ID 是在指令之前呈现的。
这些是父母:
<divtest id="1" class="divtest hidden"></divtest>
<divtest id="2" class="divtest"></divtest>
<divtest id="3" class="divtest"></divtest>
<divtest id="4" class="divtest"></divtest>
<divtest id="5" class="divtest"></divtest>
<divtest id="6" class="divtest"></divtest>
<divtest id="7" class="divtest"></divtest>
<divtest id="8" class="divtest"></divtest>
<divtest id="9" class="divtest"></divtest>
<divtest id="10" class="divtest"></divtest>
<divtest id="11" class="divtest"></divtest>
<divtest id="12" class="divtest hidden"></divtest>
</ul>
divtest 的模板是
<li><div><slider></slider></div></li>
滑块的模板是
<li class="slider-list" ng-repeat="character in range">{{character}}</li>
这是滑块指令的代码
'use strict';
angular.module('keyboardPrototypeApp')
.directive('slider', function () {
return {
templateUrl: '../../views/slider.html',
replace: false,
restrict: 'E',
controller: function($scope){
},
link: function preLink(scope, element, attrs) {
if(element.parents('li.divtest')[0]){
var index = parseInt((element.parents('li.divtest')[0].id));
var ranges = {2:[0,1,2,3,4,5,6,7,8,9 ],
3:[10,11,12,13,14,15,16,17,18,19 ],
4:[20,21,22,23,24,25,26,27,28,29 ],
5:[30,3,32,33,34,35,36,37,38,39 ],
6:[40,41,42,43,44,45,46,47,48,49 ],
7:[50,51,52,53,54,55,56,57,58,59 ],
8:[60,61,62,63,64,65,66,67,68,69 ],
9:[70,71,72,73,74,75,76,77,78,79 ],
10:[80,81,82,83,84,85,86,87,88,89 ],
11:[90,91,92,93,94,95,96,97,98,99 ]
};
scope.range = ranges[index];
}
}
};
});
如何访问该滑块指令中的父 ID 以将其用作范围数组的索引?