我試圖做一個簡單的懸停指令,我怎麼堅持我的「$ scope.selected」變量?因爲,當我點擊一個鏈接,然後點擊另一個鏈接,之前的鏈接仍然標記...我見過很多關於此的其他帖子,但沒有一個使用「部分」或指令。如何創建懸停指令
我的html:
<nav>
<ul>
<rf-hover rf-on-hover-class="hover" rf-on-hover-index="0" ng-transclude>Home</rf-hover>
<rf-hover rf-on-hover-class="hover" rf-on-hover-index="1" ng-transclude>About us</rf-hover>
<rf-hover rf-on-hover-class="hover" rf-on-hover-index="2" ng-transclude>Contact</rf-hover>
</ul>
</nav>
我angularjs指令:
app.directive('rfHover', function() {
return {
restrict: 'E',
scope: {
onHoverClass: '@rfOnHoverClass',
index: '@rfOnHoverIndex'
},
replace: true,
transclude: true,
templateUrl: "partial.html",
controller: function ($scope) {
$scope.selected = null;
$scope.clicked = false;
$scope.mouseover = function() {
$scope.selected = $scope.index;
};
$scope.mouseleave = function() {
if($scope.clicked !== $scope.index) {
$scope.selected = false;
}
}
$scope.mouseclick = function() {
$scope.selected = $scope.index;
$scope.clicked = $scope.index;
}
$scope.isSelected = function() {
return $scope.selected === $scope.index
};
}
};
});
我的部分HTML元素:
<li ng-mouseover='mouseover()' ng-mouseleave="mouseleave()" ng-click="mouseclick()"
ng-class="{active : isSelected()}"></li>
我不知道我的理解 - 什麼是你正在試圖解決這個問題? –
,當你在一個裏點擊,它接受一個類(活動),但在此之後,當你在一個地點擊,李前必須刪除類(有源)和電流I接收類(活動),但是這是不會發生什麼。 –
因此,您希望鼠標懸停時項目變爲紅色,並在鼠標懸停時恢復正常。如果你點擊任何一個項目,它會保持紅色,直到你點擊另一個項目,那麼這個應該恢復正常,其他應該變成紅色。它是否正確 ? –