I am using this controll: http://blueimp.github.io/jQuery-File-Upload/angularjs.html to do file uploading in angular, once the file is uploaded to my server I return a url and would like to display it to the client on the success callback. The plugin being used is just a normal jquery file upload but there is a angular directive wrapper provided which I'm using.
Here's how I define the callback:
$scope.options = {
url: '/api/Client/',
type: 'PUT',
done: function (event, data) {
var scope = angular.element(this).scope();
scope.test = "doesn't work";
scope.$apply(function () {
scope.test = "this doesn't work either";
});
}
};
The file uploads fine, and the done function is called however I am unable to update the view. I initially tried by just changing scope, then I realised I would require the $apply() function but that isn't working either.
I have also tried
$scope.options = {
url: '/api/Client/',
type: 'PUT',
done: function (event, data) {
$scope.test = "doesn't work";
$scope.$apply(function () {
$scope.test = "this doesn't work either";
});
}
};
and that also doesn't work. I am not sure why it isn't updating my view, and as the done call is just an ajax success event I don't see how this specific plugin could be causing any issues with $scope.$apply. I am using AngularJs 1.1.5, but I have also tried 1.0.7 and am getting the same issue.
There are a few ways to deal with this. The easiest way to deal with this is to use the built in $timeout, and a second way is if you are using underscore or lodash (and you should be), call the following: $timeout(function(){ //any code in here will automatically have an apply run afterwards });
AngularJS ScopeThe scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.
$scope. $apply() takes a function or an Angular expression string, and executes it, then calls $scope. $digest() to update any bindings or watchers.
Each AngularJS application has exactly one root scope, but may have any number of child scopes.
Copied from my comment to make it clearer what the problem was:
I managed to figure out the problem. When using their example I had a duplicate of ng-controller(Their example was nested within my other controller) and even though both were using the same controller it seems like it would only update anything that was within nested controller scope. When removing the duplicate ng-controller attribute it all works fine.
I thought I would chime in too, as I've spent a whole day dealing with this issue.
Make sure that in your HTML, you apply the ng-controller to a div wrapper, not a ul element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With