首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义对话中的孤立范围

自定义对话中的孤立范围
EN

Stack Overflow用户
提问于 2016-10-06 11:50:51
回答 1查看 32关注 0票数 2

嗨,我对自定义指令中的孤立范围很陌生。

这是我的指示

代码语言:javascript
复制
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中,我只是将它包括在

代码语言:javascript
复制
<file-browser></file-browser>

我要在指令范围内的文件

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-10 09:04:19

创建一个数组,如

代码语言:javascript
复制
scope.imageSrcForPreview = [];

并将对象插入数组中,因此我的函数看起来类似于。

代码语言:javascript
复制
var promise = fileReader.readAsDataURL(scope.fileForImagepreview, scope);
                promise.then(function(result) {
                    scope.imageSrcForPreview.splice(index, 1, result);
                    scope.showPreview = true;
                });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39895246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档