Angular 2 primary slave devices. Inter Directory Communication

I want to create a select list in 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);
   }
}

And I'm trying to use it like this:

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

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

The problem is this: liWasClicked will never be clicked!

+4
source share
1 answer

I just made a directive that does what you want: https://www.npmjs.com/package/ngx-selectable-list

This is the main directive: 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

, , .

README, : , 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 selectedConfirmed, .

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 , .

0

Source: https://habr.com/ru/post/1663752/


All Articles