I have read several posts on the use of this
instead of $scope
, and vice versa, but being fairly new to javascript in general, I feel like I'm still missing something.
Below is a code example where I'm doing a POST request
. But by the time I get inside the method, my formData
object is empty. But if I change it from this
to $scope
, it works, and I'm having a hard time understanding why that is.
code:
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);
});
};
});