Angular 2主从指令通信。指令间沟通

dal*_*ez1 5 angular2-directives angular

我想在Angular2中创建一个可选列表:

import {Directive, ElementRef, HostListener, Input, Output, EventEmitter} from "@angular/core";

@Directive({selector: 'li'})
export class ListItem {

   @Input() private selectableItem: any;
   @Output('selectedEvent') private selectedEvent: EventEmitter<any> = new EventEmitter<any>();

   constructor(private hostElement: ElementRef) {
   }

   @HostListener('click', ['$event'])
   private toggleSelected(event: MouseEvent): void {
       this.hostElement.nativeElement.classList.toggle('selected');
       this.selectedEvent.emit(this.selectableItem);
   }

}



@Directive({selector: '[selectableList]'})
export class SelectableListDirective {

   constructor(private hostElement: ElementRef) {
   }

   @HostListener('selectedEvent', ['$event'])
   private liWasClicked(event): void {
       console.log(event);
   }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试像这样使用它:

<ul selectableList>
    <li *ngFor="let item of items" [selectableItem]="item">
        <span>{{item}}</span>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

Plunker:https://plnkr.co/edit/umIE6yZwjyGGvJdYe7VS p = preview

问题是:liWasClicked永远不会被点击!

小智 2

我刚刚做了一个指令,可以满足您的需求: https ://www.npmjs.com/package/ngx-selectable-list

这是主指令: https://github.com/darkbasic/ngx-selectable-list/blob/master/src/directive/selectable-list/selectable-list.directive.ts

这是从属指令: https://github.com/darkbasic/ngx-selectable-list/blob/master/src/directive/selectable-item/selectable-item.directive.ts

他们通过共享服务进行通信,不幸的是这是唯一的方式。

我仍然没有设法为它编写自述文件,因为我遇到了一些打包问题:因此,如果您使用 angular-cli 它目前可能只能在 AOT 模式下工作。

您可以在这里找到一些使用示例

模式一:

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chat-viewer/containers/chat/chat.component.ts

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chat-viewer/components/messages-list/messages-list.component.ts

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chat-viewer/components/message-item/message-item.component.ts

在此模式下,我们仅进行多项选择,由按下事件和投影确认按钮激活。

模式2:

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chats-lister/containers/chats/chats.component.ts

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chats-lister/components/chats-list/chats-list.component.ts

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chats-lister/components/chat-item/chat-item.component.ts

现在该指令可以在单模式和多模式下工作:它监听点击事件,但它也通过按下事件激活多选模式。

模式3:

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/b941aa2202279c4e1c879125a8551b0c4649e7d8/src/app/chats-lister/containers/chats/chats.component.ts

我以前总是通过内容投影来投影确认按钮,但您可以采用不同的方式:相反,您可以侦听 selectItemIds 事件,并在用户单击您自己的确认按钮后发出 selectConfirmed 事件。

模式4:

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chats-creation/containers/new-group/new-group.component.ts

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chats-creation/components/users-list/users-list.component.ts

https://github.com/Urigo/whatsapp-client-angularcli-material/blob/master/src/app/chats-creation/components/user-item/user-item.component.ts

在此示例中,指令在 multiple_tap 模式下工作:多重选择是通过点击而不是按下事件来初始化的。

一旦我编写文档并附加一些 GIF 来展示不同的模式,一切都会变得更加清晰。