I am trying to upload a file to a cloud using the ng-file-select directive of Angular. The function called for uploading the image should be called only when the form containing the upload field is submitted. The code is given below:
<form class="form-horizontal" name="reviewEntry" role="form" ng-controller="reviewController" ng-submit="submitFeedback(reviewer.image)" novalidate>
...
<input type="file" id="image" name="image" ng-file-select="onFileSelect($files)" accept="image/*" ngf-max-size="5MB" ngf-model-invalid="errorFiles">
...
</form>
My controller code:
$scope.submitFeedback = function(file) {
$scope.showSuccess = false;
$scope.message = "Loading ...";
var review = {
name: $scope.reviewer.name,
model: $scope.reviewer.model,
image: $scope.reviewer.image,
review: $scope.reviewer.review
};
$scope.onFileSelect = function(files) {
if (files && files.length) {
$scope.file = files[0];
}
alert("ON FILE SELECT");
Upload.upload({
url: '/images/upload',
method: 'POST',
file: $scope.file
}).success(function(data) {
console.log("SUCCESS: " + data);
}).error(function(err) {
console.log("ERROR: " + err.message);
});
...
};
However, onFileSelect inside submitFeedback is never called (no alert box) - the upload should happen only after the submit button is pressed. I am relatively new to Angular - can anyone help me fix this?