替换可拖动功能我试图写在角度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({
如何替换此调用?