0

I have a PHP app that has some AngularJS pages.

I want to re-use one of the templates in a different Angular controller, and use Angular to hide some content based on values of drop downs.

I don't want to have to repeat myself and make an almost identical template with the ng-hide in there, as I'd end up making multiple copies of the content and risk things getting out of step when changes are made.

The other places where the templates are used do not and will not have the methods defined to hide and show based on the drop downs. How do I hide and show content in Angular, without using ng-hide etc? Do I need to make dummy controller methods to avoid errors? Surely there's a better way?

3
  • did you consider using ng-if ? you would not have to change your tags, just add one around Commented Dec 15, 2014 at 10:36
  • I think he included ng-show when talking about nghide, that;'s the same thing basically Commented Dec 15, 2014 at 10:37
  • Why not creating a directive that encapsulate your template ? You'll be able to reuse it as many time as you want, and there won't be any multiple copy? Commented Dec 15, 2014 at 10:51

1 Answer 1

1

You could use ng-if together with a bool variable which you only define and set to false where you want to hide that control:

<div ng-if="showTheDiv !== false"></div>

As long as $scope.showTheDiv is not defined or set to true, the content will be shown. Therefore, if you need to hide it somewhere, just be sure to set this value.


A better solution would be to encapsulate repeating parts in a directive which you then will be able to reuse, and add some parameters with reasonable defaults.

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

2 Comments

Thanks - the "if" thing makes sense here - so you can get away with checking the value of something that doesn't exist?
Yes, angular expressions are very forgiving, instead of ReferenceError and TypeError, you'll get undefined/null

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.