[英]Angularjs handle click action on element
I'm new in angularjs. 我是angularjs的新手。 I have something like this with jquery. 我的jQuery有这样的东西。
wordClick(); function wordClick() { var dblClick = false; $('#text span.word').on('click', function(){ var that = this; setTimeout(function() { if(dblClick == false){ console.log('single click event. word is "'+$(that).text()+'"'); } }, 400) }).dblclick(function() { var that = this; dblClick = true; setTimeout(function(){ dblClick = false; console.log('double click event. word is "'+$(that).text()+'"'); }, 500) }) }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre id="text"> <span class="word">This</span> <span class="word">is</span> <span class="word">word</span> <span class="word">I</span> <span class="word">want</span> <span class="word">to</span> <span class="word">click</span> <span class="word">this</span> <span class="word">word</span> </pre>
I want to handle single click and double click event. 我想处理单击和双击事件。 How to do that with angularjs? 如何用angularjs做到这一点?
use ng-click and ng-dblclick on your span elements 在跨度元素上使用ng-click和ng-dblclick
https://docs.angularjs.org/api/ng/directive/ngClick https://docs.angularjs.org/api/ng/directive/ngClick
https://docs.angularjs.org/api/ng/directive/ngDblclick https://docs.angularjs.org/api/ng/directive/ngDblclick
Your implementation is not suited for direct AngularJS migration. 您的实现不适合直接AngularJS迁移。 You have to change your HTML code a bit to populate from an array and add a single ng-dblclick
to the span
. 您必须稍微更改HTML代码以从数组中填充,然后将一个ng-dblclick
添加到span
。
This is just an example to show you how to think in Angular
way. 这只是一个示例,向您展示如何以Angular
方式思考。
var app = angular.module("app", []); app.controller("MainController", function($scope){ $scope.data = ["This", "is", "word", "I", "want", "to", "click"]; //Double click $scope.shoutLoud = function(word){ alert(word); } //Single click $scope.shout = function(word){ console.log(word); } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="MainController"> <span ng-click="shout(word)" ng-dblclick="shoutLoud(word)" ng-repeat="word in data" class="word"> {{word}} </span> <div>
Use ngClick, ngDblclick and ngRepeat in html 在html中使用ngClick,ngDblclick和ngRepeat
<pre id="text">
<span ng-click="click(word)" ngDblclick="dblClikc(word)" ng-repeat="word in words" class="word"></span>
</pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.