I am trying to create a drop down list directive that appears when the mouse hovers over the drop-down header or when the drop-down list is omitted and disappears otherwise.
I succeeded, but if the drop-down list is not closed by selecting an item or clicking on the title again, than the arrow does not disappear.
(I.E. If I open one list and open another without closing the first, than the arrow of the first list does not disappear)
JsFiddle - http://jsfiddle.net/rpg2kill/uS4Bs/
the code:
var myApp = angular.module('myApp', ['ui.bootstrap']); function MyCtrl($scope) { $scope.supportedList= ['Option1', 'Option2', 'Option3', 'Option4']; $scope.selectedItem = 'Option1'; } myApp.directive('dropDown', function () { return { restrict: 'E', replace: false, scope: { supportedList:'=', selectedItem:'=' }, template: '<div ng-mouseenter="onMouseEntered()" ng-mouseleave="onMouseLeft()">' + '<a class="dropdown-toggle" data-toggle="dropdown" href="" ng-click="onMouseClicked()" >' + '<img ng-style="{\'visibility\': dropDownIconVisibility}" src="http://png.findicons.com/files/icons/2222/gloss_basic/16/arrow_down.png"> </img>' + //Arrow down Icon '<span>{{selectedItem}}</span>' + '</a>' + '<ul class="dropdown-menu">' + '<li ng-repeat="item in supportedList" ng-click="onSelectedItem(item)">' + '{{item}}' + '</li>' + '</ul>' + '</div>' , link: function(scope, element, attrs) { scope.dropDownIconVisibility = "hidden"; scope.dropDownIconVisibilityLocked = false; scope.onSelectedItem = function(item) { scope.dropDownIconVisibilityLocked = false; scope.selectedItem = item ; }; scope.onMouseEntered = function() { scope.dropDownIconVisibility = "visible"; }; scope.onMouseLeft = function() { if (scope.dropDownIconVisibilityLocked) return; scope.dropDownIconVisibility = "hidden"; }; scope.onMouseClicked = function() { scope.dropDownIconVisibility = "visible"; scope.dropDownIconVisibilityLocked = !scope.dropDownIconVisibilityLocked; }; } }; })
The code is a little ugly. The best solution is to show the arrow if the mouse hangs OR , the list opens, but I do not know how to associate angular with the state of the drop-down list.
Is there a way to attach angular to twitter bootstrap popup event? Or is there a better way to solve this problem?
source share