It's the first time I'm using Angular's watch function and apparently I don't get it to work.
I have a service called apiService
, which has a variable myFile
. I am injecting the service into my controller and want to watch the apiService.myFile
value for a change. Unfortunately the watch only gets called on opening the webpage, not when the apiService.myFile
variable actually changes. Here is the code for the watch:
$scope.$watch(function(){return apiService.myFile}, function (newVal, oldVal, scope) {
console.log("service changed: "+newVal +" : "+oldVal+" : "+ scope);
});
Why isn't it being called when myFile
changes?
UPDATE 1:
this is how I update the value of apiService.myFile
inside the service
ApiService.prototype.uploadFile = function(fileContent) {
$.ajax({
url: "/space_uploadFile/",
type: "POST",
dataType: "json",
data: {
fileContent: fileContent
},
contentType: "application/json",
cache: false,
timeout: 5000,
complete: function() {
//called when complete
console.log('process complete');
},
success: function(data) {
this.myFile =data;
console.log("this.myFile: "+this.myFile);
console.log('process success');
},
error: function() {
console.log('process error');
},
});
};