Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you pass a bound variable to an ng-click function?

I have a simple delete button that will accept a string or number but won't accept an ng-model variable ( not sure if that's the correct terminology ).

<button class="btn btn-danger" ng-click="delete('{{submission.id}}')">delete</button> 

Which generates:

<button class="btn btn-danger" ng-click="delete('503a9742d6df30dd77000001')">delete</button> 

However, nothing happens when I click. If I hard code a variable then it works just fine. I assume I'm just not doing things the "Angular" way, but I'm not sure what that way is :)

Here's my controller code:

$scope.delete = function ( id ) {     alert( 'delete ' + id ); } 
like image 884
ccraig Avatar asked Sep 28 '12 21:09

ccraig


People also ask

Can we put condition in NG-click?

We can add ng-click event conditionally without using disabled class.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.


1 Answers

You don't need to use curly brackets ({{}}) in the ng-click, try this:

<button class="btn btn-danger" ng-click="delete(submission.id)">delete</button> 
like image 52
pkozlowski.opensource Avatar answered Sep 29 '22 21:09

pkozlowski.opensource