我无法访问div中的li元素。我将mouseover事件添加到li,但它仅绑定到其父div。
<div>
<ul class="rating">
<li ng-repeat="star in stars" ng-class="star" ng-click="toggle($index)"
ng-mouseover="starHover($index)" id="$index"> \u2605'
</li>
</ul>
</div>
scope.starHover = function(index)
{
elem.addClass('unfill').removeClass('filled');
}
我想访问我鼠标悬停的li和nextAll li也是。
答案 0 :(得分:4)
不要在范围内进行,在指令中执行。像这样更改您的li
标记:
<li ng-repeat="star in stars" ng-class="star" ng-click="toggle($index)" id="$index" mousable > <!-- ... --> </li>
并创建mousable
这样的指令:
yourApp.directive('mousable', function () {
return {
link: function(scope, element, attrs , ctrl) {
element.bind("mouseover", function(e){
element.addClass('unfill').removeClass('filled');
});
element.bind("mouseout", function(e){
element.addClass('filled').removeClass('unfill');
});
}
}
}
参考文献:
http://docs.angularjs.org/api/angular.element
http://docs.angularjs.org/guide/directive
答案 1 :(得分:1)
以角度方式做到:
<div>
<ul class="rating">
<li ng-repeat="star in stars" ng-class="{star: true, unfill: hovered, filled: !hovered}" ng-click="toggle($index)"
ng-mouseenter="hovered = true" ng-mouseleave="hovered = false" id="$index"> \u2605'
</li>
</ul>
</div>