19

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>
2
  • 4
    Is there any special reason that you are not using onclick? Using ng-click should be fine Commented Sep 3, 2015 at 13:58
  • In my case, the reason to use onclick instead of ng-click is to use a function inside a Utility class I reuse in multiple projects. This way I don't have rewrite the function into every controller. Commented Mar 29, 2019 at 16:08

10 Answers 10

27

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

2
  • i am facing a similar issue. I could list out the reasons I don't want to use the scope... but I really don't get why angular can't just allow "onclick" like any other attribute Commented Sep 8, 2016 at 16:38
  • 2
    There are many reasons why he might want to use an onclick instead of ng-click, we most obvious being that he is relying on a large javascript library which is not written in Angular.
    – Leon
    Commented Sep 27, 2016 at 13:21
7

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>
4
  • Awesome! Thanks a lot. The reason that I can't use ng-click is in <uib-accordion-header>. In this accordion, header is default to expand/collapse content. In order to prevent expand/collapse and have custom action, I have to use ng-click to handle $event.preventDefault() and $event.stopPropagation() then onclick for my custom action.
    – Khanh Tran
    Commented Oct 22, 2018 at 21:26
  • this would work for a single variable, how would one do this with the "item in items" as example in a ng-repeat? Send the complete "item"
    – Ewald Bos
    Commented Nov 7, 2019 at 9:40
  • FYI, I had a situation where I had a directive that could only be triggered by clicking it, but I needed to actually trigger it by clicking a different element. Calling document.querySelector('#directive-id').click() in the console worked, but when I moved that script into an ng-click function on the element I actually needed to click it threw a bunch of scope errors. Turned out using <element-to-click id="{{directive-id}}" onclick="document.querySelector('#' + this.id).click()"> worked perfectly! Thanks! Commented Apr 13, 2020 at 14:54
  • You can add all arguments like this by introducing your own attributes ; p1,p2,p3: <button p1="{{data.SysUserGuid}}" p2="{{data.SysUserSecurityStamp}}" p3="{{data.PayAmount}}" onclick="bringupBuyMCoinsConsumer(this.getAttribute('p1'),this.getAttribute('p2'),this.getAttribute('p3'));" >Buy</button> Commented May 23, 2020 at 9:52
3

Im not going to second guess your reasons for not using ng-click, as other contributors have pointed out you really 'ought'. However if you really want/need to, heres my suggestion by using 'this' and data attributes.

<div data-filterListId="{{filterList.id}}" onclick="deleteArrival(this)" class="table-icon deleteIcon">{{filterList.id}}</div>
function deleteArrival(arrivalElem) {
    alert('myId=' + arrivalElem.getAttribute("data-filterListId"));
}
1
  • if someone needs to pass data out of scope of angularjs this answer could be helpful
    – Mazdak
    Commented Jul 18, 2019 at 17:41
2

You could easily solve your problem using ng-click but you should have deleteArrival method in your scope.

Markup

<div ng-click="deleteArrival(filterList.id)" class="table-icon deleteIcon">
  {{filterList.id}}
</div>
5
  • Yes, I suggest him to use ng-click as he is using angularJS. Commented Sep 3, 2015 at 13:58
  • i need to use onclick('hi'); like this. but the data has been get from ng-repeat.i understood your idea. but i need like this.. I want to call javascript function.so that i ask it bro
    – uday s
    Commented Sep 3, 2015 at 13:59
  • @gauravbhavsar I added comment because you said No need to use {{}} inside ng-click Though the user has used onclick Commented Sep 3, 2015 at 14:00
  • @PankajParkar ahh ! Thanks Commented Sep 3, 2015 at 14:02
  • @gauravbhavsar Thats look better now +1 Commented Sep 3, 2015 at 14:07
1

Above thing is easily possible using ng-click directive and having that function inside controller scope, only the thing is you need to assign your java-script function reference to controller scope variable. No need to rewriting the function in your scope again. Pass the reference of function will do the trick.

Markup

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

Controller

//assign javascript method reference in controller
$scope.deleteArrival = deleteArrival;
1

You are not allowed to create a binding for event handler attributes like onclick, onload, onsubmit, etc. in angularjs because, there is no practical value in binding to these attributes and doing so it only exposes your application to security vulnerabilities like XSS. For these reasons binding to event handler attributes (all attributes that start with on and formaction attribute) is not supported in angularjs.

For your case,

Inside ng-repeat, use ng-click for sending values to your function and declare that function in controller.

See here for documentation of ng-click

Hope this helps !

1
  • there IS practical value , at least for my case. onclick="legacyOrGlobalMethod({{product.id}})" Commented Oct 29, 2016 at 16:00
0

try this without the curly braces deleteArrival(filterList.id)

0
0

I had a case where I could not use ng-click due to window binding. Did not get direct answer but the below did work for me. I know it is not a good solution but workable in my case.

angular.element(this).scope().ctrlName.variable

You can try using your own class structure. So the click will be as below:

onclick="test(angular.element(this).scope().ctrlName.ObjName.variable)"

0

   $scope.getpop = function(id){
       alert(id);
    }
<span ng-click='getpop({{x.Code}})' class="btn-default btn">{{ x.title }}</span> 

<b>This works perfect for me !</b>

0

My use case was where my application is relying on a large JavaScript file of functions that are not written in Angular. For this, I added a function in my controller that connects to one of these JS functions:

$scope.controllerFunction = function() {
  javaScriptFunction('foo');
}

javascriptFunction(parameter){
  let variable = parameter
} 

Then in my view, making use of ng-click, I accessed the controller function with:

 ng-click="controllerFunction()"

For me, the "onclick="deleteArrival(this.id)" example returns undefined. The above was the best solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.