1
我想創建一個指令,監視被拖拽到一個元素上的文件。 我的問題是,我似乎無法觸發scope.files 我還試圖建立一個隔離的範圍表:看繼承範圍上創建的對象
scope:{files:'='}
,然後做
scope.$watch('files',function(){})
鏈接功能內,但沒有運氣。
如何讓手錶拿起正在更新的文件?
我添加了「scope.files = []」,因爲當手表設置時它最初爲空。
angular.module('myApp').directive('dragDrop', [function() {
return {
link: function (scope, element, attrs) {
scope.files = [];
scope.$watch(scope.files, function() {
if (scope.files !== undefined && scope.files.length === 1) alert("a file has been dropped");
});
element.on('dragenter', function (e) {
e.stopPropagation();
e.preventDefault();
$(this).css('border', '2px solid #0B85A1');
});
element.on('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
});
element.on('drop', function (e) {
$(this).css('border', '0px');
e.preventDefault();
//var files = e.originalEvent.dataTransfer.files;
scope.files = e.originalEvent.dataTransfer.files;
});
}
};
嗯,是的,我看這是我失蹤了,但不幸的是這並沒有導致手錶被觸發。 – blomster
@MichaelBlom因爲你的文件變量是一個基元,如果你把它改變成一個複雜的對象,子範圍不會影響當前的項目,但如果你在子範圍中使用原始對象創建新的實例...檢查我的答案更新我添加一個例子.. –
感謝您花時間幫助@ wickY26,您的掠奪者最終將我帶到了那裏。 – blomster