jquery - Watch another elements classes with AngularJS -
i use bootstrap javascript components create toggleable panels. each panel class in
set, if opened. i'd show font awesome icon depending on current panel state.
fa fa-angle-up
when it's open, , fa fa-angle-down
when it's closed.
however have difficulties achieving it, can't figure out proper angularjs way it. current approach using jquery hasclass()
method check class , apply correct font awesome class arrow.
as jquery doesn't support two-way-databinding has issues. i've figured is, method called once , proper class set arrow, when open/close panel ng-class
doesn't re-evaluated switch it.
what correct "angular-way" achieving this?
<div class="panel panel-default"> <div class="panel-heading" role="tab" id="headlineedit"> <h4 class="panel-title"> <a class="collapsed" data-toggle="collapse" data-target='#collapseedit' href="#collapseedit" aria-expanded="false" aria-controls="collapseedit"> <div class="leftside">edit</div> <div class="rightside arrow-show"><span class="fa" ng-class="$('#collapseedit').hasclass('in') ? 'fa-angle-up' : 'fa-angle-down'"></span></div> <div class="clear"></div> </a> </h4> </div> <!-- "in" class determines whether panel opened or closed --> <div id="collapseedit" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headlineedit"> <div class="panel-body"> placeholder text </div> </div> </div>
here's way directive:
app.directive('test',function(){ return{ link: function(scope, el, attr) { angular.element(el[0].queryselector('.rightside > .fa')).addclass('fa-angle-up'); el.on('hide.bs.collapse', function(ev){ angular.element(el[0].queryselector('.rightside > .fa')) .removeclass('fa-angle-up') .addclass('fa-angle-down'); }); el.on('show.bs.collapse', function(ev){ angular.element(el[0].queryselector('.rightside > .fa')) .removeclass('fa-angle-down') .addclass('fa-angle-up'); }); } } });
and remove ng-class
attribute html.
here's plunker: http://plnkr.co/edit/xo8oqc68zrwtiha5ajdi?p=preview
Comments
Post a Comment