1

I have a directive that i only want active should an element have a certain class. for this i have attempted the following:

<feedback-analytics ng-if="$('#analyticTab').hasClass('active')"></feedback-analytics>

However when the class of the element changes the element is not displayed.

Has anyone done something similar and made it work?

4
  • If you want to show/hide your element, you should use "ng-show" or "ng-hide" instead, see: stackoverflow.com/questions/21869283/… Commented Mar 18, 2016 at 12:25
  • @Aliz i need the directive to be rendered completely from new thats why i use ng-if - its a hack for a bug in the Chartjs lib Commented Mar 18, 2016 at 12:26
  • You could fire an event when the class appear on your element "analyticTab" (with a directive and $watch), and in the controller of your "feedback-analytics" if you catch the event you re-render your directive Commented Mar 18, 2016 at 12:34
  • Why isn't jQuery tagged or mentioned in the title of the question? This is not purely an Angular question. Commented Nov 28, 2017 at 10:03

1 Answer 1

3

Try something else:

In controller add a scope.$watch that watches over: $('#analyticTab').hasClass('active'). Anytime when the value is changed, it will call this watch.

Then create a scope variable of boolean type that will be false or true, depends on above condition.

In view change your code to this: <feedback-analytics ng-show="yourBooleanVariable"></feedback-analytics>

Sign up to request clarification or add additional context in comments.

2 Comments

This would work 100% but im not using controllers and $scope as they will partly disappear in Angular 2 and using them is no longer best pratice :s
I see. Well I will think about something and if I can find something I will tell you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.