Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular conditional container element

I have a large chunk of HTML in an ng-repeat that for certain elements has a container element and for others it does not. I'm currently achieving this with two ng-ifs:

<strike ng-if="elem.flag">
  … <!-- several lines of directives handling other branching cases -->
</strike>
<div ng-if="!elem.flag">
  … <!-- those same several lines copied-and-pasted -->
</div>

While this works, it means I have to remember copy-and-paste any edits, which is not only inelegant but also prone to bugs. Ideally, I could DRY this up with something like the following (inspired by ng-class syntax):

<ng-element="{'strike':flag, 'div':(!flag)}">
  … <!-- lots of code just once! -->
</ng-element>

Is there any way to achieve a similarly non-repetitive solution for this case?

like image 483
eirikir Avatar asked Dec 08 '15 06:12

eirikir


1 Answers

  1. You can make such directive yourself.
  2. You can use ng-include to include the same content into both elements.
like image 174
Qwertiy Avatar answered Sep 19 '22 09:09

Qwertiy