0

I have some existing JavaScript I need to call. My existing function is called 'confirmDelete'. For the sake of demonstration, the shortened version looks like this:

function confirmDelete(orderId) {
  return confirm('Are you sure you want to delete Order #"' + orderId + '"?');
}

There is more to this function. Either way, I am trying to call this function like this:

<a href="~/order/delete/{{order.Id}}" onclick="return confirmDelete('{{order.Id}}');">delete order</a>

My other bindings are working. However, I cannot figure out how to pass the order ID to my existing JavaScript function. How do I do that?

Thank you!

1
  • 2
    why don't you define the confirm function a in scope and replace onclick with ng-click ? Commented Oct 29, 2014 at 18:53

3 Answers 3

1

You could inject $window:

.controller('ExampleController', ['$scope', '$window', function($scope, $window) {
    $scope.window = $window;
});

and then you can do:

<a href="~/order/delete/{{order.Id}}" ng-click="window.confirmDelete(order.Id);">delete order</a>

Or you can add

$scope.confirmDelete = function(id) {
    return window.confirmDelete(id);
}

And then you can do:

<a href="~/order/delete/{{order.Id}}" ng-click="confirmDelete(order.Id);">delete order</a>

Or you could create a pass-through method for any global function:

$scope.callGlobal(fn, params) {
    if (!angular.isArray(params)) {
        params = [params];
    }
    return window[fn].apply(window, params)
}

Which takes the function name, and an array of params (or, if just one argument, you can pass just the argument), and then you can do

<a href="~/order/delete/{{order.Id}}" ng-click="callGlobal('confirmOrder', order.Id);">delete order</a>
Sign up to request clarification or add additional context in comments.

Comments

0

Use the ng-click directive instead of the non-Angular onclick handler, and pass the $event object as well.

In the controller:

 $scope.confirmDelete = function(orderId, $event) {
   if (!confirm('Are you sure you want to delete Order #"' + orderId + '"?')) {
      $event.preventDefault();
   }
 }

Then you can reference it in your html with

     <a href="~/order/delete/{{order.Id}}" ng-click="confirmDelete(order.Id, $event)">delete order</a>

here's a working Plunker: http://plnkr.co/edit/p8Bm0WI7skVZFH3rhTun?p=preview

Also, you could actually just leave the href="..." out and handle the view change in the controller function as well.

2 Comments

and if you left the href="..." out and handle the change in the controller, then you wouldn't have to deal with the $event object at all, since you wouldn't have to deal with preventing the default click behavior.
another clarification: the extra $event object code is only required if your href="..." is a server route, vs. an Angular one (i.e. href="#something" vs. the href="~/something" like you showed). If you're doing a server route, as you showed in your question, you have to prevent the click from following the href in the case where "cancel" is selected.
0

Instead of

<a href="~/order/delete/{{order.Id}}" onclick="return confirmDelete('{{order.Id}}');">delete order</a>

try

<a href="~/order/delete/{{order.Id}}" ng-click="return confirmDelete(order.Id);">delete order</a>

I made a fiddle , although you should use globals like i did

Updated fiddle, as per Mike said

2 Comments

I don't think that will work, since it's not suppressing the default click behavior when the user click "cancel". The fiddle is working because the href="..." was changed from href="~/order/..." to href="#/{{item.id}}". If you change the href back to a server route, it will follow it regardless. This would have the effect of deleting the order no matter how the user responds to the "Are you sure..." confirmation.
oh, i see, thatswhy you added the preventDeault ! @Mike

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.