替換可拖動功能我試圖寫在角度1 *的角指令轉變爲角2.在角2
這裏是原始指令代碼:
this.directiveModule.directive('draggable', function(CONFIG) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
var setInitPosition;
setInitPosition = function() {
return elem.css({
top: scope.position.top + 'px',
left: scope.position.left + 'px'
});
};
elem.draggable({
containment: "parent",
stop: function(ev, ui) {
return scope.position = {
top: ui.position.top,
left: ui.position.left
};
}
});
return setInitPosition();
}
};
});
我有如下轉化爲角2指令:
@Directive(
{
selector: "[draggable]"
}
)
export class DraggableDirective implements OnInit{
@Input() position: any;
@Output() setPosition: EventEmitter<any> = new EventEmitter(); // assign position to scope.designElement
constructor(private elem: ElementRef){}
setInitPosition() {
this.elem.nativeElement.css({
top: this.position.top + 'px',
left: this.position.left + 'px'
});
};
ngOnInit(){
this.elem.nativeElement.draggable({
containment: "parent",
stop: this.stopDrag
});
this.setInitPosition();
}
stopDrag(ev, ui){
let position = {
top: ui.position.top,
left: ui.position.left
};
this.setPosition.emit(position);
}
}
但是,當我使用這個新指令的元素,我得到一個錯誤說elem.nativeElement不包含可拖動的功能在以下在ngOnInit()函數中行。
this.elem.nativeElement.draggable({
如何替換此調用?