14

<div ng-if="true">visible</div> is pretty easy, but since ngIf can be used even in comments, what would be the closing </div> for comment block?

Tried, w/o luck:

<!-- ng-if: true -->
....
<!-- ng-if -->

Thanks.

Aleksandr Makov
  • 2,820
  • 3
  • 37
  • 62
  • I don't know where you heard/read that but it's not true. ng-if is a directive that needs to be added as an attribute in an element. – Wawy Apr 11 '14 at 13:04
  • 1
    I don't think you want this tagged `ngif`. "NGif is an open-source tool that creates the ability for .Net to make/read Animated Gifs" – Sean the Bean Jun 01 '15 at 17:23
  • @SeantheBean thanks, corrected. – Aleksandr Makov Jun 02 '15 at 12:39
  • 1
    It's worth noting you can achieve a similar result using `ng-if-start="true"` and `ng-if-end` [see this stack overflow question](http://stackoverflow.com/questions/29252098/how-to-apply-an-ng-if-or-other-conditional-in-multiple-td-elements-while-kee) – Matt Oakley Feb 17 '16 at 10:40

1 Answers1

33

ng-if is restricted to 'A'. so it can be used only as attribute, you can't use in comment Here's the angularjs code for ngIf

var ngIfDirective = ['$animate', function($animate) {
  return {
    transclude: 'element',
    priority: 600,
    terminal: true,
    restrict: 'A',       // --> This means restricting to Attribute

The restrict option is typically set to: 'E','A','C','M'

One of EACM restricts the directive to a specific directive declaration style. If you don't restrict any, the defaults (elements and attributes) are used.

E - Element name (default): <my-directive></my-directive>

A - Attribute (default): <div my-directive="exp"></div>

C - Class: <div class="my-directive: exp;"></div>

M - Comment: <!-- directive: my-directive exp -->

Vamsi
  • 9,510
  • 6
  • 38
  • 46