[英]Send dynamic data in $http.post from $scope in angularjs
My template can be found here http://goo.gl/xEttgl . 我的模板可以在http://goo.gl/xEttgl中找到。 I am hardcoding postData by declaring it as a global variable as
我通过将其声明为全局变量来对postData进行硬编码
var postData = {'result': '27'};
app.factory('mypostService', function($http) {
return {
postFooOldSchool: function() {
$http.post('/angularresult', JSON.stringify(postData)
).success(function(data, status, headers){
}).error(function(data, status, headers){
});
}
}
});
app.controller('StartUpController', function($scope, $http, myService, mypostService) {
$scope.upvotes = 0;
$scope.serverupvotes = 0;
$scope.increase = function(){
$scope.upvotes = $scope.upvotes + 1;
};
mypostService.postFooOldSchool(function(data) {
});
myService.getFooOldSchool(function(data) {
$scope.result = data;
});
});
but what i would like to do is send postData dynamically from $scope inside StartUpController ie lets say i click upvote button and i want to send the current value of $scope.upvotes in POST and display it in the result. 但我想做的是从StartUpController内的$ scope中动态发送postData,即可以说我单击了upvote按钮,我想在POST中发送$ scope.upvotes的当前值,并在结果中显示它。
I over complicated the problem by using a factory, I found a simple solution, instead of using factory for POST i can directly send an $http.post request from the Controller. 我通过使用工厂使问题复杂化了,我找到了一个简单的解决方案,而不是将工厂用于POST,我可以直接从Controller发送$ http.post请求。 A sample solution is as follows
示例解决方案如下
$scope.increase = function(){
$scope.upvotes = $scope.upvotes + 1;
var x = $scope.upvotes;
alert(x)
var postObject = {'result': x};
$http.post('/angularresult', JSON.stringify(postObject)).success(function(data){
});
myService.getFooOldSchool(function(data) {
$scope.result1 = data;
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.