<分区>
我已经阅读了几篇关于使用 this
而不是 $scope
的文章,反之亦然,但总的来说,作为 javascript 的新手,我觉得我'我仍然缺少一些东西。
下面是我执行POST 请求
的代码示例。但是当我进入方法内部时,我的 formData
对象是空的。但是,如果我将它从 this
更改为 $scope
,它就会起作用,但我很难理解为什么会这样。
代码:
var app = angular.module('TM', []);
app.controller('tableController', function($scope, $http){
//Empty object that gets filled with data and sent as a POST request.
this.formData = {};
// Define process for submitting form
//TODO: FIND OUT THE DIFFERENCE BETWEEN $SCOPE AND THIS
this.processForm = function() {
console.log('Inside processForm method');
$('#addEntry').modal('hide');
console.log($scope.formData); //If I use this here - the object is empty
$http({
method : 'POST',
url :'../protocols',
data : JSON.stringify($scope.formData),
headers : {
'dataType' : "json",
'contentType' : "application/json"
}
})
.success(function(data) {
console.log('Success: ' + data);
//Empties the object after the POST request is done...
$scope.formData = {}
})
.error(function(data){
console.log('Error ' + data);
});
};
});