0
\$\begingroup\$

Is there any easier/shorter/better way to do the marked part?

plnkr

<body ng-app="">
  Apple <input type="checkbox" ng-model="apple" aria-label="Toggle ngHide"><br/>
  Banana  <input type="checkbox" ng-model="banana" aria-label="Toggle ngHide"><br/>
<div>
  <!-- This part-->
  <div ng-show="apple || banana">
    You want to buy:
    <span> 
        <span ng-show="apple">Apple</span>
        <span ng-show="banana">Banana</span>
    </span>
  </div>
  <!-- This part-->
</div>
</body>
\$\endgroup\$
2
\$\begingroup\$

Better and more performance oriented solution will be to use ng-if instead of ng-show. Although it doesn't matter with such few items on scope but with huge/big data its better to use ng-if.

<body ng-app="">
  Apple <input type="checkbox" ng-model="apple" aria-label="Toggle ngHide"><br/>
  Banana  <input type="checkbox" ng-model="banana" aria-label="Toggle ngHide"><br/>
<div>
  <div ng-if="apple || banana">
    You want to buy:
    <span> 
        <span ng-if="apple">Apple</span>
        <span ng-if="banana">Banana</span>
    </span>
  </div>
</div>
</body>
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I was thinking more syntactically if you could make it shorter \$\endgroup\$ – Viktor Mellgren Aug 11 '15 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.