Last active
August 29, 2015 14:08
-
-
Save luismasuelli/cd67ff4c790ae8419283 to your computer and use it in GitHub Desktop.
Angular File Upload - Crea la directiva file-model con soporte para envio de multipart y dependencia de jqueryfilestyle. La directiva ng-model NO banca file uploads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
angular | |
.module('FileUploads', []) | |
.factory('FileUploads.FormDataBuilder', ['$window', function(window) { | |
return function(data) { | |
var fd = new window.FormData(); | |
angular.forEach(data, function(value, key) { | |
console.log(value); | |
fd.append(key, value); | |
}); | |
return { | |
data: fd, | |
opts: { | |
transformRequest: angular.identity, | |
headers: { | |
'Content-Type': undefined | |
} | |
} | |
}; | |
} | |
}]); | |
.directive('jfilestyle', function(){ | |
return { | |
restrict: 'A', | |
scope: { | |
options: '=jfilestyle' | |
}, | |
controller: ['$scope', function($scope){ /* vacio, para tirar error */ }], | |
link: function(scope, element, attrs/*, controller(s)*/) { | |
angular.element(element).jfilestyle(scope.options); | |
} | |
} | |
}) | |
.directive('fileModel', ['$parse', function ($parse) { | |
return { | |
restrict: 'A', | |
require: 'jfilestyle', | |
link: function(scope, element, attrs) { | |
var model = $parse(attrs.fileModel); | |
var modelSetter = model.assign; | |
scope.$watch(attrs.fileModel, function(value){ | |
if (!value) { | |
element.jfilestyle('clear'); | |
} | |
}); | |
element.bind('change', function(){ | |
scope.$apply(function(){ | |
modelSetter(scope, element[0].files[0]); | |
}); | |
}); | |
} | |
}; | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment