angular5.0 direcitve 使用问题,html页面找不到指令
来源:4-3 打造支持拖拽的属性型指令

Maych
2017-12-16
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DragDirective } from './drag-drop/drag.directive';
import { DropDirective } from './drag-drop/drop.directive';
@NgModule({
imports: [
CommonModule
],
declarations: [
DragDirective,
DropDirective
],
exports:[
DragDirective,
DropDirective
]
})
export class DirectiveModule { }
=============================================
import {Directive, Input, ElementRef, Renderer2, HostListener} from '@angular/core';
@Directive({
selector: '[appDraggable][draggedClass]',
})
export class DragDirective {
private _isDraggable = false;
@Input() draggedClass: string;
@Input('appDraggable')
set isDraggable(draggable: boolean) {
this._isDraggable = draggable;
this.rd.setAttribute(this.el.nativeElement, 'draggable', `${draggable}`);
}
get isDraggable() {
return this._isDraggable;
}
constructor(
private el: ElementRef,
private rd: Renderer2,
) {
}
@HostListener('dragstart', ['$event'])
onDragStart(ev: Event) {
console.log(6789)
if (this.el.nativeElement === ev.target) {
this.rd.addClass(this.el.nativeElement, this.draggedClass);
}
}
@HostListener('dragend', ['$event'])
onDragEnd(ev: Event) {
if (this.el.nativeElement === ev.target) {
this.rd.removeClass(this.el.nativeElement, this.draggedClass);
}
}
}
===================================================
shared.module中也导入导出DirectiveModlue了,就是页面不能使用
不知道哪个环节出问题了,看老师5.1的代码和我的directive并没有什么区别。实在找不出原因,麻烦老师帮忙分析一下
1回答
-
Maych
提问者
2017-12-16
老师,刚才解决了。
这个地方的selector要用逗号隔开,不明白你的5.1的没用逗号隔开可以使用,我的5.0得用逗号隔开才能用。这个太坑了啊。
还有这个方法是什么时候调用的
get isDraggable() {
return this._isDraggable;
}
我注释了也不影响使用。
012018-01-05
Angular打造企业级协作平台,让你在Angular领域中出类拔萃
998 学习 · 536 问题
相似问题
回答 2
回答 3