ajay dixit ajay dixit - 1 month ago 8
AngularJS Question

isolated scope in custom diretive

Hi I am new to isolated scope in custom directive

This is my directive

restrict: 'E',
scope: {},
template: '<input type="file" ng-model="myFile" />{{myFile.name}}',
link: function(scope, element, attrs, sharedServices) {
var model = $parse(attrs.fileBrowser);
var modelSetter = model.assign;
element.bind("change", function(e) {
scope.fileForImagepreview = (e.srcElement || e.target).files[0];

if (scope.fileForImagepreview.type === "image/jpeg" ||
scope.fileForImagepreview.type === "image/jpg" ||
scope.fileForImagepreview.type === "image/png") {
scope.$apply(function() {
modelSetter(scope, element[0].firstChild.files[0]);
});
}

var promise = fileReader.readAsDataURL(scope.fileForImagepreview, scope);
promise.then(function(result) {
scope.imageSrcForPreview = result;
});
});
}


and in html i just include it as

<file-browser></file-browser>


I want file in scope of directive

Answer

create one array like

scope.imageSrcForPreview = [];

and insert the object into the array,so my function looks like.

var promise = fileReader.readAsDataURL(scope.fileForImagepreview, scope);
                promise.then(function(result) {
                    scope.imageSrcForPreview.splice(index, 1, result);
                    scope.showPreview = true;
                });
Comments