1

I have a button in a directive that I only want to appear on the last object in the array. The ng-show evaluates an expression on the controller.

<button class="btn btn-danger button-xs tsid-btn-sch-pad
    glyphicon glyphicon-remove"
            type="submit" ng-click=""
            ng-show="{{$index == sc.schedule.length - 1}}"></button>

The expression is being correctly evaluated in the browser, but the button is displaying anyway.

enter image description here

So the difference between the highlighted row and the one above where the delete button is not displaying is that ng-hide was added to the class attribute of the row above and it has not been added to the row that is displaying the delete button incorrectly. But I don't know why that update isn't taking place since the ng-show expression is being updated.

pthalacker
  • 2,056
  • 3
  • 20
  • 37
  • First guess is to add parenthesis to your condition, second one to remove the {{}} I don't think they are mandatory within a ng-show – Flavien Volken Dec 17 '14 at 19:34

1 Answers1

0

Try using the $last variable

<button class="btn btn-danger button-xs tsid-btn-sch-pad
    glyphicon glyphicon-remove"
            type="submit" ng-click=""
            ng-show="$last"></button>

http://plnkr.co/edit/Sf6Xw7YjPlfUuqyHIWng?p=preview

Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
  • Nope. Same results. The problem is not that the expression isn't being evaluated correctly. It is that the ng-hide class attribute is not getting added to the directive that used to be last. It must have something to do with the digest loop and the scope. But I don't know what. – pthalacker Dec 17 '14 at 21:55
  • @pthalacker then it should be another problem from your project context, I added a plunker for the proof of concept – Flavien Volken Dec 18 '14 at 06:30
  • @Flavian Volken Please forgive me, but I don't see how that plnkr relates. It displays the first 9 letters of the alphabet, but I can't see any user interaction with it. – pthalacker Dec 19 '14 at 00:36
  • @pthalacker check again, I saved instead of forking another project by error on plnkr, you were then landing on the wrong version one. I fixed the link. – Flavien Volken Dec 21 '14 at 18:25
  • @Flavian Volken Your plnkr achieves the same results as I get as far as it goes. My problem starts when I add a fourth object to the array on the client. The fourth object gets the last button, but the button does not disappear from the third object. I could not figure out how to add a fourth object to your plnkr. – pthalacker Dec 22 '14 at 18:08
  • @pthalacker check the plnkr, I added an action on the button which will add an item… no issue here so far – Flavien Volken Dec 23 '14 at 13:37
  • @Flavian Volken Okay, that will give me something to work with. I have changed the design of the UI, so the offending code no longer exists. I will pull the code out of source control and look again when I get the chance. I appreciate the your efforts. Thank you. – pthalacker Dec 23 '14 at 17:19