2016-12-14 73 views
4

我想創建Angular2可選列表:Angular 2主從指令通信。國米指令通信

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); 
    } 
} 

而且我嘗試使用它,像這樣:

<ul selectableList> 
    <li *ngFor="let item of items" [selectableItem]="item"> 
     <span>{{item}}</span> 
    </li> 
</ul> 

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

問題是:liWasClicked永遠不會被點擊!

+0

我在這裏遇到了同樣的問題,我無法使用子指令與父指令進行通信。任何人都可以幫忙 – soywod

+0

@soywod如果你的指令有模板,事件有效,但在我的情況下,它們不會,所以事實並非如此。 – dalvarezmartinez1

回答

0

我剛剛作出了一個指令,它做你想要什麼: 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模式下工作。

您可以在這裏找到一些使用的實例

模式1:

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事件,併發出,一旦用戶點擊一個selectionConfirmed事件在您自己的確認按鈕上。

模式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模式下工作:多重選擇由抽頭而不是按壓事件initializated。

一旦我編寫文檔並附加一些GIF來顯示不同的模式,一切都會變得更加清晰。