Friday 15 February 2013

Binding AngularJS value to call existing JavaScript function -



Binding AngularJS value to call existing JavaScript function -

i have existing javascript need call. existing function called 'confirmdelete'. sake of demonstration, shortened version looks this:

function confirmdelete(orderid) { homecoming confirm('are sure want delete order #"' + orderid + '"?'); }

there more function. either way, trying phone call function this:

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

my other bindings working. however, cannot figure out how pass order id existing javascript function. how do that?

thank you!

you inject $window:

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

and can do:

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

or can add

$scope.confirmdelete = function(id) { homecoming window.confirmdelete(id); }

and can do:

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

or create pass-through method global function:

$scope.callglobal(fn, params) { if (!angular.isarray(params)) { params = [params]; } homecoming window[fn].apply(window, params) }

which takes function name, , array of params (or, if 1 argument, can pass argument), , can do

<a href="~/order/delete/{{order.id}}" ng-click="callglobal('confirmorder', order.id);">delete order</a>

javascript angularjs

No comments:

Post a Comment