Angularjs 自定义目录中的隔离作用域
嗨,我是自定义指令中隔离作用域的新手 这是我的指令Angularjs 自定义目录中的隔离作用域,angularjs,Angularjs,嗨,我是自定义指令中隔离作用域的新手 这是我的指令 restrict: 'E', scope: {}, template: '<input type="file" ng-model="myFile" />{{myFile.name}}', link: function(scope, element, attrs, sharedServices) { var model = $parse(attrs.fileBro
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;
});
});
}
restrict:'E',
作用域:{},
模板:“{myFile.name}}”,
链接:功能(范围、元素、属性、共享服务){
var模型=$parse(attrs.fileBrowser);
var modelSetter=model.assign;
元素绑定(“更改”,函数(e){
scope.fileForImagepreview=(e.srcElement | | e.target).files[0];
如果(scope.fileForImagepreview.type==“图像/jpeg”||
scope.fileForImagepreview.type==“图像/jpg”||
scope.fileForImagepreview.type==“图像/png”){
作用域$apply(函数(){
modelSetter(作用域,元素[0].firstChild.files[0]);
});
}
var promise=fileReader.readAsDataURL(scope.fileForImagepreview,scope);
承诺。然后(功能(结果){
scope.imageSrcForPreview=结果;
});
});
}
在html中,我只是将其作为
<file-browser></file-browser>
我想要指令范围内的文件创建一个数组,如
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;
});
});
}
scope.imageSrcForPreview = [];
然后将对象插入数组,使我的函数看起来像
var promise = fileReader.readAsDataURL(scope.fileForImagepreview, scope);
promise.then(function(result) {
scope.imageSrcForPreview.splice(index, 1, result);
scope.showPreview = true;
});
您应该将
sharedServices
注入指令函数,然后它将在链接函数中对您可用。看看这个答案,这可能会给你一个概述。虽然我不知道你为什么要把它创建为孤立的?你打算在不同的屏幕上多次使用此选项吗?这里的具体问题是什么?@charlietfl有两个输入字段具有相同的型号名称,如果我从第一个输入浏览图像,它将在选择后预览,第二个相同,最后将预览两个不同的图像。好的..这是一个目标。。。现在来谈谈问题。。什么是有效的还是无效的?你有错误吗?