1

I'm trying to show or hide a div using ng-if directive in AngularJS. It works but it still appears in the html source code.

How can I completely remove the div from the html? This is how I'm doing it:

<div ng-if="user != null && user != 'Error'">CONTENT</div>

Thanks!

3
  • How do it? by action? Commented Feb 27, 2016 at 17:55
  • ng-if is meant to add/remove the element from the DOM, so what you're doing should work. Can you post your code? Commented Feb 27, 2016 at 17:55
  • I just edited the post with the piece of code I have. Thanks! Commented Feb 27, 2016 at 17:58

1 Answer 1

3

How can I completely remove the div from the html?

AngularJS' ng-if Places a comment wrapping your element, so if ng-if evaluates to false, your element is removed but it is marked with the comment.

AngularJS runs on the client, so your template(html) will look how you code it. It evaluates and manipulates your template on the browser, unlike MVCs that you can do with ASP MVC and PHP which will construct the html on the server and send it to the client 'ready', AngularJS needs the 'raw' template, and do the manipulation in the client side. So looking in the view source will not show you how angularjs manipulated your template, you can look at it using the developer tools

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

2 Comments

I'm not sure if I understand. If I go to the view-source for example in chrome I don't see any comment in the html. I see the same div tag I posted above.
Great! No problem :)

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.