Reputation: 4692
I'm uploading a file and I have validation logic in an MVC controller for the file. This attachment process can be used in the entire application; therefore, I need a directive.
I want to be able (once I click on the Upload button) to call an MVC controller from an Angular directive to eventually send the file to a web service.
Can someone please tell me how I can accomplish this?
I want to be able to use something like the below code inside my directive:
$http.post(ajaxUrl)
.success(function (status) {
if (status.Succeed) {
alert('file uploaded'
}
else {
alert(status.ErrorResult);
}
}
Upvotes: 0
Views: 509
Reputation: 2131
Did you consider to create and inject service to your directive?
(function () {
'use strict';
angular
.module('app')
.factory('service', service);
function service($http) {
var url = 'url';
var service = {
add: add
};
return service;
function add(data, callback, errorCallback) {
$http({
method: 'POST',
url: url,
data: data
})
.success(function (data, status, headers, config) {
})
.error(function (data, status, headers, config) {
});
};
}
})();
Call of the service in directive
service.add(data, function (status) {
});
Upvotes: 1