0

There is a button which must show a toggle only if a value is greater than 0. Otherwise it shouldn't do anything.

This is the code before adding ng-if:

<span >{{values.valuesNumber}} 
      <i class="fas fa-caret-down" ng-click="$ctrl.doSomething(values)">
      </i>
</span>

I want to call the function from ng-click only when values.valuesNumber > 0.

So I added it like the following but it does not work.

 <span >{{values.valuesNumber}} <i class="fas fa-caret-down">
     <span ng-if="values.valuesNumber > 0">
         <span ng-click="$ctrl.doSomething(values)">
         </span>
     </span>
 </i></span>
Leo Messi
  • 5,157
  • 14
  • 63
  • 125

1 Answers1

0

use ng-show instead of ng-if. Because ng-if creates a scope of it's own.

<span >{{values.valuesNumber}} <i class="fas fa-caret-down">
     <span ng-show="values.valuesNumber > 0">
         <span ng-click="$ctrl.doSomething(values)">
         </span>
     </span>
 </i></span>

Also please take a look at this discussion:

what is the difference between ng-if and ng-show/ng-hide

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • "Don't use _A_, use _B_" is not the best solution – Aleksey Solovey May 17 '18 at 13:24
  • Had you read this from the link which is providing in my answer? **When an element is removed using ngIf its scope is destroyed and a new scope is created when the element is restored. The scope created within ngIf inherits from its parent scope using prototypal inheritance.** . This is thr problem with `ngIf`. So the solution is `ng-show/hide` – Ramesh Rajendran May 17 '18 at 13:32
  • you can also reach parent's scope with `$parent` – Aleksey Solovey May 17 '18 at 13:34
  • @AlekseySolovey Yeah! that's also a better way to solve this. I am just answered it for an another option. Because I saw your comments and you have mentioned there. So i don't want to answer that. :) – Ramesh Rajendran May 17 '18 at 13:36
  • **"Don't use A, use B"** . If A does not working, then there have no problem for go with B. – Ramesh Rajendran May 17 '18 at 13:37