Couldn't able to pass the anchor tag id value through ng-click in Angular JS. Getting undefined value

StackOverflow https://stackoverflow.com/questions/23563430

سؤال

I'm a newbie in AngularJS please help me out to find the issue , code shown below ( html code part - 1 with js code part-2) is working perfectly in my browser and I'm getting the alert.

Html Code

<a  id={{data.id}} ng-click='onclickof("data")' class="item item-icon-left" >

               {{data.name}} + {{data.id}}
</a>

Function defined on controller

$scope.onclickof= function(idinput){
  alert(idinput);
  }

But when I tried to pass the anchor tag id to my function its giving alert with 'undefined' string. Please check the code below.

Html code which passing id of anchor tag.

<a  id={{data.id}} ng-click='onclickof(this.id)' class="item item-icon-left" >

                {{data.name}} + {{data.id}}
  </a>

Can anyone help me to find out the issue . Thanks in advance... :)

هل كانت مفيدة؟

المحلول

Just try ng-click='onclickof(data.id)'

You can also use your first example but without quotes like ng-click='onclickof(data)'

نصائح أخرى

Yes, you will just have to this

ng-click='onclickof(data.id)'  

'this.id' will not work because it is not under the $scope.

For more information regarding this please check:
https://docs.angularjs.org/guide/scope

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top