简体   繁体   English

AngularJS,链接单击不起作用

[英]Angularjs, link click not working

This might sound like a stupid question but I can't seem to figure out the answer, I'm studying angularjs these days and I've successfully generated the page and they have links that looks like this. 这听起来像是一个愚蠢的问题,但是我似乎无法弄清楚答案,这些天我正在研究angularjs,并且我已经成功生成了页面,并且它们具有如下所示的链接。

<div ng-repeat="evsi in evz" class="ng-scope">
<a href="#" likey="{{evsi.key}}" id="sideli" class="ng-binding">Link text</a>
</div>

What I want to do is when the link is clicked I need to retrive the likey value attached to it and pass it where an event is triggered. 我想做的是单击链接后,我需要检索附加到它的likey值,并将其传递给触发事件的位置。

So I used the following code 所以我用下面的代码

$('#sideli').on('click', function(){
    var linkkey = $(this).data('likey');
});

However this doesn't get the likey value i needed, Even when i set up a simple alert event to check whether the link is clicked the alert doesn't show up. 但是,这并没有获得我需要的值,即使我设置了一个简单的警报事件来检查是否单击了链接,警报也不会显示。 Where is the error and how can I fix it? 错误在哪里,我该如何解决?

Thanks 谢谢

in you html: 在你的HTML:

<a ng-click="doWhatYouWant(evsi)" likey="{{evsi.key}}" 
           id="sideli" class="ng-binding" href="javascript:void(0)">Link text</a>

in your controller: 在您的控制器中:

$scope.doWhatYouWant = function(evsi){
   console.log(evsi.key);
}

Keep in mind you are using angular. 请记住,您正在使用角度。 There is no need to query for elements and bind a click event. 无需查询元素并绑定click事件。

You could use ngClick for that. 您可以为此使用ngClick。

<div ng-repeat="evsi in evz" class="ng-scope">
  <a href="#" ng-click="linkkey = evsi.key">Link text</a>
</div>

If you want to retrive value of attribute, use attr method: 如果要检索属性值,请使用attr方法:

$('#sideli').on('click', function(){
     var linkkey = $(this).attr('likey');
});

But better way is to use angular, as @Michael suggested. 但更好的方法是使用@Michael建议使用angular。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM