Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass angular js variable inside onclick function as parameter

I have tried to pass AngularJS variable as argument value inside onclick() to call javascript function. Can anyone guide me on how to do it?

My code:

 <div onclick="deleteArrival({{filterList.id}})" class="table-icon deleteIcon">{{filterList.id}}</div>
like image 541
uday s Avatar asked Sep 03 '15 13:09

uday s


People also ask

Can we pass parameter in onClick function?

It's very easy to Pass parameter to JavaScript function using the onClick() function. If sending a string value then use double” ” or single ” quote in the function.

How do you pass a value on onClick event?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

What is $$ in AngularJs?

The $ in AngularJs is a built-in object.It contains application data and methods.

Can we use onClick in angular?

the Angular 2 onClick EventThe Angular 2 event system can be used to handle different types of events, such as mouse clicks, keyboard presses, and touch gestures. The onclick event triggers an event or function when a user clicks on a component or an element.


2 Answers

You should be using ng-click, there is no reason to use onclick as angular provides you with this functionality

<div ng-click="deleteArrival(filterList.id)" 
     class="table-icon deleteIcon">{{filterList.id}}</div>

You should then move your function into your AngularJS Controller, and bind it to the scope

$scope.deleteArrival = function(filterListId) { ... };

If you ABSOLUTELY need to use onclick to call an external function, you could change the function to something like this in your scope, still using the ng-click attribute above:

$scope.deleteArrival = function(filterListId) { window.deleteArrival(filterListId); };

However I can't see a reason not to move it into your scope

like image 56
SixteenStudio Avatar answered Oct 05 '22 23:10

SixteenStudio


If you still want to use onclick , this is work for me , I hope it work for you too.

<div id="{{filterList.id}}" onclick="deleteArrival(this.id)" class="table-icon deleteIcon">{{filterList.id}}</div>
like image 44
Pichet Thantipiputpinyo Avatar answered Oct 05 '22 22:10

Pichet Thantipiputpinyo