0
md-button.md-warn(ng-click="$ctrl.remove(); reload()" ng-hide="$ctrl.isNew") Delete

The delete button removes a file from a list, but it is not deleting the preview of the file. If the page is refreshed, the preview disappears, which is what I want. So I want the delete button to refresh the page as well.

ng-click is set to two functions as seen above.

But when 'Delete' is clicked, remove() occurs, but the page does not reload. Why is reload() not getting carried out? I tried the following as well:

md-button.md-warn(ng-click="$ctrl.remove(); $ctrl.reload()" ng-hide="$ctrl.isNew") Delete

2 Answers2

1

If you're trying to refresh the browser window, you want to be calling window.location.reload(), not just a bare reload() (which will try to run a function named "reload" on the directive scope), or $ctrl.reload() (which will try to run a function named "reload" on the controller scope, which isn't what you want to do.) (The other answers seem to have missed the part where you said that you're trying to refresh the browser window.)

But you're using Angular, so you definitely don't want to reload the whole browser window just to make a preview go away. That's a brute force solution that goes against many of the reasons to use a SPA framework at all.

Instead just reset whatever state variable made the preview appear in the first place.

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
-1
md-button.md-warn(ng-click="$ctrl.remove() || $ctrl.reload()" ng-hide="$ctrl.isNew") Delete

Need to chain functions like this in angularjs

bryan60
  • 28,215
  • 4
  • 48
  • 65
  • 1
    That's not correct. See https://stackoverflow.com/questions/16813945/how-to-add-many-functions-in-one-ng-click for example – Daniel Beck Aug 08 '17 at 18:18
  • oh, just the way I've always done it then. Both work just fine. – bryan60 Aug 08 '17 at 18:36
  • That's not correct either. Combining functions with `||` willl not run the second function if the first one returns anything truthy: https://jsfiddle.net/g0h4jLyf/ – Daniel Beck Aug 08 '17 at 18:45
  • yes, in those cases you use &&... but the kind of functions you run in templates shouldn't be returning values. The only reason they would is if you want to conditionally chain. Which this provides for, and in that case, if you get past one simple operator, you shouldn't be chaining it in templates. – bryan60 Aug 08 '17 at 19:10
  • There's no advantage to using AND or OR here, other than subjecting yourself to the risk of buggy behavior whenever a return value changes (and/or confused code maintainers, who will naturally assume there's some logical reason the functions are ANDed or ORed together). Just use a semicolon if all you need is to separate statements; that's what it's for. – Daniel Beck Aug 08 '17 at 19:19
  • if you're subjecting yourself to risky or buggy behavior by using and / or operators, then you shouldn't be binding those functions to templates in the first place. Again, template bound functions have no reason to return values other than to control conditional chaining in templates, they should be simple and predictable. I reiterate, both are perfectly valid methods of chaining functions, but one provides more functionality than the other. Call the advantage that it forces you to write better code that doesn't bind complex function calls to templates. – bryan60 Aug 08 '17 at 19:25
  • We're clearly headed towards "agree to disagree" here, so I'm going to let it rest. Cheers. – Daniel Beck Aug 08 '17 at 19:41