0

I have been trying to addClass through AngularJS and the code doesn't seem to work, weird thing is addClass is working on Parent Menu Item but doesn't work on Sub item.

I have a nested UL and LI, when I click on the Parent LI ParentLi function gets called and it adds a "focused" class to the Clicked LI, this works fine but when I click on Nested LI's I call childLi and I do the same operation as done for the Parent but class doesn't get added. I am new to Angular and I hope I am doing this in the right way.

$scope.parentLi = function(event) {
    var liElement = angular.element(event.target.parentNode);
    var allParentLiElements = document.getElementsByClassName('parent-dropdown');
    if (!liElement.hasClass('focused')) {
        angular.element(allParentLiElements).removeClass('focused');
        liElement.addClass('focused');
    } else
        liElement.removeClass('focused');
};

$scope.childLi = function(event){
    var liElement = angular.element(event.target.parentNode);
    var allParentLiElements = document.getElementsByClassName('child-dropdown');
    if(!liElement.hasClass('focused')){
        angular.element(allParentLiElements).removeClass('focused');
        $(event.target).closest('.parent-dropdown').addClass('focused');
        liElement.addClass('focused');
    } else
        liElement.removeClass('focused');
}

Note that i have edited my jsfiddle code based on the answer given by Jiam30.

adding focused class should work like active class i.e the menu that i just clicked should have focused class other should not, same way if i have hover on menu item and click on subitem, both the subitem and the parent item should have focused class.

Fiddle

1
  • Have you looked at using ng-class for a pure angular approach to this?
    – jbrown
    Commented Mar 21, 2016 at 10:54

1 Answer 1

3

Manipulating elements in a controller should be avoided.

Use ng-class instead (also use ng-repeat to avoid HTML repetition). For instance:

<li class="dropdown parent-dropdown" ng-click="parentLi()" ng-class="{'focused': isDropdownFocused}"></li>

With this function in the controller:

$scope.parentLi = function() { 
    $scope.isDropdownFocused = !$scope.isDropdownFocused;
};

Updated Fiddle: http://jsfiddle.net/6be56/127/

5
  • i am using ng-repeat only in actual code, i just copied content from console. Item that you have declared i get it from server and i have used factory for that. Commented Mar 21, 2016 at 11:54
  • one more point i want to note here is i have multiple Items in my parent Menu like Net, i have 5 more, how do i remove the focused class for them if they have it? Commented Mar 21, 2016 at 12:00
  • Store the 6 booleans telling you if they are focused somewhere in the $scope and modify those booleans in your controller at will. You might want to factorize your menus in a directive if they behave the same.
    – Jiam30
    Commented Mar 21, 2016 at 12:05
  • i cannot do like that, the problem its a data driven menu, the number of items in menu may varry depends on the application. Commented Mar 21, 2016 at 12:14
  • I don't know much of angular but for your reference i am using this menu github.com/sirajc/dynamic-menu Commented Mar 21, 2016 at 12:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.