我想将用户活动发送到服务器。在我的my previous question中,我询问了ng-click
的情况。这个问题是关于复选框和单选按钮的情况。AngularJS:发送复选框/单选按钮值到服务器
我想发送到服务器哪个复选框或单选按钮用户点击记录用户活动。
我知道我可以这样做。
HTML:
<!-- for checkbox -->
<div class="btn-group">
<label class="btn btn-primary" ng-model="checkModel.left" uib-btn-checkbox>Left</label>
<label class="btn btn-primary" ng-model="checkModel.right" uib-btn-checkbox>Right</label>
</div>
<!-- for radio button-->
<div class="btn-group">
<label class="btn btn-primary" ng-model="radioModel" uib-btn-radio="'Left'">Left</label>
<label class="btn btn-primary" ng-model="radioModel" uib-btn-radio="'Right'">Right</label>
</div>
JS:
$scope.$watch('checkModel', function() {
sendServer("checkModel" + JSON.stringify(checkModel));
// Do other things for checkModel
}, true);
$scope.$watch('radioModel', function() {
sendServer("radioModel" + radioModel);
// Do other things for radioModel
});
function sendServer(msg) {
var req = new XMLHttpRequest();
var params = "msg=" + encodeURIComponent(msg);
req.open("POST", "/scripts/log");
req.send(params);
}
这样要求我插入sendServer
所有watch
。这是低效的。我想以综合的方式来做。我可以做吗?
您可以用'directive'这个 –
你可以参考这里:https://stackoverflow.com/questions/45767704/angularjs-send-user-activity-to-server?noredirect=1&lq=1 –
使用[AngularJS $ http服务](https://docs.angularjs.org/api/ng/service/$http)代替原始的'XMLHttpRequest'。 '$ http'服务是一个核心的AngularJS服务,它通过浏览器的[XMLHttpRequest API](https://developer.mozilla.org/en/xmlhttprequest)来促进与远程HTTP服务器的通信。 – georgeawg