Skip to content

Instantly share code, notes, and snippets.

@possamai
Forked from anonymous/AngularUpload.js
Last active August 27, 2015 00:33
Show Gist options
  • Save possamai/765c390b3e9155da286e to your computer and use it in GitHub Desktop.
Save possamai/765c390b3e9155da286e to your computer and use it in GitHub Desktop.
angular.module('App').directive('gallery',function(){
return {
templateUrl:'/admin/angular/templates/gallery.html',
replace:true,
scope:{
gallery:'='
},
controller:['$scope','$http',function(scope,http){
scope.button = 'Selecione uma imagem';
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress',function(evt){
scope.button = parseInt(evt.loaded/evt.total*100)+'%';
if(!scope.$$phase) scope.$apply();
});
xhr.addEventListener('load',function(evt){
console.log(evt);
scope.button = 'Selecione uma imagem';
scope.gallery.push(JSON.parse(evt.target.responseText));
if(!scope.$$phase) scope.$apply();
});
scope.send = function(file){
var fd = new FormData();
fd.append('data[file]',file);
fd.append('data[model]','Noticia');
xhr.open('POST',base+'/administrar/gallery/receive');
xhr.send(fd);
}
scope.remove = function(foto){
http.post(base+'/administrar/gallery/delete',{id:foto.id}).success(function(evt){
if(evt.status == 'ok'){
scope.gallery = scope.gallery.filter(function(curr){
return curr.id != foto.id ;
});
}
});
}
}]
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment