从angularjs中的$scope在$http.post中发送动态数据
Send dynamic data in $http.post from $scope in angularjs
我的模板可以在这里找到http://goo.gl/xEttgl.我通过将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;
});
});
但我想做的是从StartUpController内的$scope动态发送postData,也就是说,我点击upvote按钮,我想在POST中发送$scope.upvotes的当前值,并将其显示在结果中。
我使用工厂使问题过于复杂,我找到了一个简单的解决方案,而不是使用工厂进行POST,我可以直接从控制器发送$http.POST请求。样品溶液如下
$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;
});
};
相关文章:
- 下载使用POST数据动态生成的文件
- post()向服务器动态生成的数据返回空响应
- 如何使用带有动态接受好友按钮的ajax post更新更多的表
- 从angularjs中的$scope在$http.post中发送动态数据
- 在 asp.net c# 中通过 POST 传递动态列表
- POST 表单动态生成的输入
- 从通过 echo 语句动态添加的表单元素中获取 POST 值
- PHP:如何使用 jQuery 和 $post[] 获取动态添加的输入的值
- 通过 POST 从动态表传递数据
- 访问 MVC 3 中动态创建的文本框的 POST 值
- 在 $.post - JQuery 中创建动态变量赋值
- 具有动态数据的高图表 $http.post 角度 js
- 通过PHP将Javascript动态HTML输入POST(每个添加的输入都需要重命名字段's)
- 如何为XHR POST参数设置动态URL
- 在动态输入区域jquery中获取post数组以插入批代码点火器
- 从javascript到php的动态url POST
- 查找要使用http-post方法发送到登录的动态生成的参数值
- 动态表单Post无法在mozilla中工作
- 将POST方法集成到动态创建的表单中
- 向ajax post请求添加动态表单数据