2

I used to work with backbone and handlebars and was able to do things like

{{ if condition }}
  <div class="container">
{{ endif }}

  more html

{{ if condition }}
  </div>
{{ endif}}

I know I can use ng-if but I want to display conditionally an element opening and closing not the whole element.

4
  • 1
    angular only deals with full elements when working within html source, not html fragments. Use directives for more advanced templating Commented Sep 13, 2014 at 15:12
  • yup, write a directive Commented Sep 13, 2014 at 15:18
  • Anyway, that's sound like a very bad thing to do! Commented Sep 13, 2014 at 17:56
  • Possible duplicate of Make a render condition with AngularJS Commented Mar 23, 2016 at 16:06

2 Answers 2

2

You can write a directive and accomplish exactly what you want, but I think a better way to do things is to create a div(without conditions - since every open div element has to have a closing element) and to add dynamic content inside using ng-if:

<div>
  <div ng-if="cond1"></div>
  <div ng-if="cond2"></div>
  <!-- ETC...... -->
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

The simplest and best way to do this by using "ng-if" attribute for any HTML element that you want to render on the basis of some condition that you can manupulate in your controller as per your need.

For example:

<div ng-if = "expression">
</div>

This expression should be evaluate to be boolean i.e. either true or false, here you can specify multiple condition also which should be evaluate to true or false.

For multiple condition example check out this fiddle: http://jsfiddle.net/9Ymvt/820/

Check out this example in example section of "ngif doc":

https://docs.angularjs.org/api/ng/directive/ngIf

Comments

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.