Javascript 如何访问指令中父级的预呈现ID,以便在ng repeat中使用?
我试图使用指令“slider”父级的ID来迭代数组范围[ID],但该ID在指令之前呈现 他们是父母:Javascript 如何访问指令中父级的预呈现ID,以便在ng repeat中使用?,javascript,angularjs,angularjs-directive,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Directive,Angularjs Ng Repeat,我试图使用指令“slider”父级的ID来迭代数组范围[ID],但该ID在指令之前呈现 他们是父母: 如何访问该slider指令中的父级ID以将其用作范围数组的索引?我认为您只需要htmlElement.getAttribute(“ID”),但使用该ID似乎有点奇怪,我可能更喜欢使用一些自定义属性数据listId=“1”或者类似的东西我创建了一个名为index的自定义属性,但是我不能通过指令访问它,就像我可以访问ID一样。我可以访问ID,但是当我使用它作为ng repeat中范围的索引时,它看
如何访问该slider指令中的父级ID以将其用作范围数组的索引?我认为您只需要htmlElement.getAttribute(“ID”),但使用该ID似乎有点奇怪,我可能更喜欢使用一些自定义属性数据listId=“1”或者类似的东西我创建了一个名为index的自定义属性,但是我不能通过指令访问它,就像我可以访问ID一样。我可以访问ID,但是当我使用它作为ng repeat中范围的索引时,它看不到它。啊,对于这一部分,我想你需要对索引进行字符串化,否则它会寻找一个类似数组的数字索引,实际上,您正在访问存储有数组的属性名。。。或者在第二次查看时,不要解析它
<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>
<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];
}
}
};
});