[英]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,我可以直接從Controller發送$ 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;
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.