嗨,我对自定义指令中的孤立范围很陌生。
这是我的指示
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;
});
});
}在html中,我只是将它包括在
<file-browser></file-browser>我要在指令范围内的文件
发布于 2016-10-10 09:04:19
创建一个数组,如
scope.imageSrcForPreview = [];并将对象插入数组中,因此我的函数看起来类似于。
var promise = fileReader.readAsDataURL(scope.fileForImagepreview, scope);
promise.then(function(result) {
scope.imageSrcForPreview.splice(index, 1, result);
scope.showPreview = true;
});https://stackoverflow.com/questions/39895246
复制相似问题