AngularJS:将用户活动发送到服务器 [英] AngularJS : send user activity to server
本文介绍了AngularJS:将用户活动发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将用户活动发送到服务器,尤其是用户单击了哪个按钮.
I would like to send user activity to server, especially which button user clicked.
我知道我可以这样做.
HTML:
<button type="button" class="btn btn-primary" ng-click="pressA()">A</button>
<button type="button" class="btn btn-primary" ng-click="pressB()">B</button>
JS:
$scope.pressA = function() {
sendServer("A");
// Do other things for button A
}
$scope.pressB = function() {
sendServer("B");
// Do other things for button B
}
function sendServer(msg) {
var req = new XMLHttpRequest();
var params = "msg=" + encodeURIComponent(msg);
req.open("POST", "/scripts/log");
req.send(params);
}
但是这种方式要求我将sendServer
插入所有ng-click事件.好烦我可以用这种更有效的方法吗?
But this way require me to insert sendServer
to all ng-click event. It is bothersome. Can I do this more efficient way?
推荐答案
如果要为定义的每个ng-click
执行某些操作,则可以定义一个自定义的ngClick
指令来完成此工作.
If you want to execute something for each ng-click
you define, you can define a custom ngClick
directive to do the job.
.directive('ngClick', function() {
return {
restrict: 'A',
priority: 1, // built-in ngClick has priority 0
link: function(scope, element, attr) {
element.on('click', function() {
// do something
});
}
}
});
但是,这不会涵盖所有操作(aHref,ngSubmit ...).
However, this will not cover every actions (aHref, ngSubmit...).
这篇关于AngularJS:将用户活动发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文