Wayne Li Wayne Li - 4 months ago 8
AngularJS Question

How to post a scope variable right after it has changed the value?

I am learning Angularjs and want to use $http.post() method.
In HTML

In Javascript

$scope.onevariable ='value1'
$scope.doSomething = function(){
$scope.onevariable = 'value2';
$http.post('someurl',{onevariable:$scope.onevariable }).then(function(){...})}


The problem is that I want to post the changed $scope.onevariable as parameter to the server, and this variable has to be changed after I click the button. However,this $scope.onevariable can be posted before it has changed to 'value2', so how can I post it exactly after the value has been changed. 'value2' is unknown value to me,or an encrypted string, you can treat it as a random string.

Answer

I think $watch would be an option

$scope.$watch('onevariable', function(newValue, oldValue) {
  $http.post('someurl',{onevariable:newValue }).then(function(){...})}
});

More about $watch

Comments