1

I'm trying to add a button that only shows for particular index numbers:

            <tbody data-ng-repeat="(wholesalerIndex,wholesaler) in wholesalers">
                <tr>
                    <td>
                        <button ng-if="$index != 0 || $index != 3" class="btn btn-danger" data-ng-click="removeWholesaler(wholesaler, wholesalerIndex)">Up</button>
                        <button class="btn btn-danger" data-ng-click="removeWholesaler(wholesaler, wholesalerIndex)">Down</button>
                    </td>
                </tr>
            </tbody>

ng-if isn't working. It works fine when I have 1 condition ng-if="$index != 0", but when I try to add the OR (||) statement it stops working.

What am I doing wrong and how do I fix it?

2
  • Just a thought - perhaps the order of operations is getting screwed up. Try using parenthesis like this: ($index != 0) || ($index != 3) Commented Apr 24, 2017 at 17:16
  • 1
    Actually - it looks like the logic is off. If Index equals 3, it is not equal to 0 so it passes. If the index equals 0, it's not equal to 3 so it passes. If index = 10000 it is neither equal to 0 (so it passes) nor equal to 3 (so it also passes) Commented Apr 24, 2017 at 17:18

2 Answers 2

1

My guess is the logic is off. Try logical and instead of logical or.

<button ng-if="$index != 0 && $index != 3" class="btn btn-danger" data-ng-click="removeWholesaler(wholesaler, wholesalerIndex)">Up</button>
Sign up to request clarification or add additional context in comments.

2 Comments

This solves it. But why would that qualify as true when index is only zero at the moment?
As angular iterates through each wholesaler the $index is set. ng-if tests that $index as it passes through the loop. If the index is 0, the "$index != 3" test will pass because 0 is not 3. Vice-versa when $index is 3, it will pass the "$index != 0" test. By using logical AND, the overall test indicates that $index may not be zero AND $index may not be three. Otherwise, the test is truthy.
0

Just use ng-show it will work :)

<tbody data-ng-repeat="(wholesalerIndex,wholesaler) in wholesalers">
    <tr>
        <td>
            <button ng-show="$index != 0 || $index != 3" class="btn btn-danger" data-ng-click="removeWholesaler(wholesaler, wholesalerIndex)">Up</button>
            <button class="btn btn-danger" data-ng-click="removeWholesaler(wholesaler, wholesalerIndex)">Down</button>
        </td>
    </tr>
</tbody>

I hope that this helps.

2 Comments

wholesaler index is an internal primary key which is different.
Right! Try using the ng-show directive instead of ng-if

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.