Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap- highlight Active and Angular directive

I am trying to make selected item on Bootstrap navbar highlighted. Here is my code:

<ul  class="nav navbar-nav navbar-left" top-bar>

and directive:

a.directive('topBar', function () {
return {
    restrict: 'AE',
    link: function (scope, elem, attrs) {
        $(elem).on("click", function () {
            $(elem).find(".active").removeClass("active");
            $(this).parent().addClass("active");
        });
    }
}

});

It only works if I remove class="nav navbar-nav navbar-left". Otherwise, it's not getting into the directive. Any suggestions?

Thanks

like image 334
Mark Avatar asked Nov 09 '22 05:11

Mark


1 Answers

I guess you want to listen clicks on list items (tag li) in the component and update its classes. Try to use event delegation – if you are using jQuery, just add extra selector into on call:

a.directive('topBar', function () {
    return {
        restrict: 'AE',
        link: function (scope, elem, attrs) {
            elem.on("click", "li", function (e) {
                elem.find(".active").removeClass("active");
                $(this).addClass("active");
            });
         }
    };
});
like image 192
just-boris Avatar answered Nov 15 '22 05:11

just-boris