请放纵,这是我的第一篇文章。
我正在编写我的第一个MEAN应用程序,并且我想使用已编写的代码。 我想使用包含app.directive函数的自定义angular.js,并且我想知道如何将此函数转换为Typescript custom.directive.ts
这是自定义拖动功能:
var app = angular.module('myApp',[]);
app.run([function () { }]);
app.controller('MainController', ['$scope', function ($scope){
$scope.image = null;
$scope.imageFileName = '';
$scope.uploadme = {};
$scope.uploadme.src = '';
}]);
app.directive('fileDropzone', function() {
return {
restrict: 'A',
scope: {
file: '=',
fileName: '='
},
link: function(scope, element, attrs) {
var checkSize,
isTypeValid,
processDragOverOrEnter,
validMimeTypes;
processDragOverOrEnter = function (event) {
if (event != null) {
event.preventDefault();
}
event.dataTransfer.effectAllowed = 'copy';
return false;
};
validMimeTypes = attrs.fileDropzone;
checkSize = function(size) {
var _ref;
if (((_ref = attrs.maxFileSize) === (void 0) || _ref === '') || (size / 1024) / 1024 < attrs.maxFileSize) {
return true;
} else {
alert("File must be smaller than " + attrs.maxFileSize + " MB");
return false;
}
};
isTypeValid = function(type) {
if ((validMimeTypes === (void 0) || validMimeTypes === '') || validMimeTypes.indexOf(type) > -1) {
return true;
} else {
alert("Invalid file type. File must be one of following types " + validMimeTypes);
return false;
}
};
element.bind('dragover', processDragOverOrEnter);
element.bind('dragenter', processDragOverOrEnter);
return element.bind('drop', function(event) {
var file, name, reader, size, type;
if (event != null) {
event.preventDefault();
}
reader = new FileReader();
reader.onload = function(evt) {
if (checkSize(size) && isTypeValid(type)) {
return scope.$apply(function() {
scope.file = evt.target.result;
if (angular.isString(scope.fileName)) {
return scope.fileName = name;
}
});
}
};
file = event.dataTransfer.files[0];
name = file.name;
type = file.type;
size = file.size;
reader.readAsDataURL(file);
return false;
});
}
};
});
app.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
scope.$apply(function () {
scope.fileread = loadEvent.target.result;
});
};
reader.readAsDataURL(changeEvent.target.files[0]);
});
}
};
}]);
我想创建一个自定义指令,如:
import { Directive } from '@angular/core';
@Directive({
selector: '[appFileDropzone]'
})
export class FileDropzoneDirective {
constructor() { }
}
任何想法或简单的链接? 我使用的是Angular-cli。