Ich habe eine Richtlinie, die die Drag & Drop von Dom Elemente erlaubt, die:Wie Angular Richtlinie auf das DOM-Element in Javascript erstellt Set
JS:
angular.module('animationApp').directive('myDraggable', ['$document', function($document){return{link: function(scope, element, attr) {
var startX = 0, startY = 0, x = 0, y = 0;
console.log("In myDraggable directive code");
element.css({
position: 'relative',
cursor: 'pointer'
});
element.on('mousedown', function(event) {
// Prevent default dragging of selected content
event.preventDefault();
startX = event.pageX - x;
startY = event.pageY - y;
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
});
function mousemove(event) {
y = event.pageY - startY;
x = event.pageX - startX;
element.css({
top: y + 'px',
left: x + 'px'
});
}
function mouseup() {
$document.off('mousemove', mousemove);
$document.off('mouseup', mouseup);
}
}};}]);
Nun, ich kann es verwenden und funktioniert perfekt (Drag/Drop-Funktionalität), wenn ich ein DOM-Element auf der HTML-Seite erstellen und die spezifische Anweisung zuweisen. Wie,
HTML:
<div my-draggable = "">Hello</div>
<div my-draggable = "">Sid</div>
Das Problem ist, ich bin nicht in der Lage, die Richtlinie auf jene Elemente zuweisen ich in Javascript-Code bin zu schaffen. Wie,
JS:
var tempView = document.getElementById('ngView');
var div = document.createElement("div");
var t = document.createTextNode("This is a paragraph.");
div.appendChild(t);
div.setAttribute("my-draggable","");
tempView.appendChild(div);
Die Richtlinie nicht funktioniert in diesem Fall!
Was ist das Problem?
Was habe ich falsch gemacht?
Danke!
Vielen Dank! es hat für mich funktioniert –