I would go for a pure Angular component!
It's pretty forward to solve.
First I would create a "MemberListComponent", just guessing from your object list. I would roughly create a component like to component below. I like to implement OnInit and OnDestroy. I don't know your setup, but you might be able to refactor more logic into the directive. I like to collect my subscribtions in OnInit and unsubscribe in OnDestory.
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { Subscription } from 'rxjs/Rx';
@Component({
selector:'memberlist',
templateUrl:'memberlist.html',
styleUrls: ['memberlist.scss']
})
export class MemberListDirective implements OnInit, OnDestroy {
@Intput() members: Array<MemberModel>;
@Output() clicked = new EventEmitter();
constructor() {
}
ngOnInit() {
}
ngOnDestroy() {
}
memberClicked(type: string, member: MemberModel) {
this.clicked.emit({
type: type,
member: member
});
}
}
In the template (memberlist.html), would I setup a click event and delegate the event to the subscribers.
<div *ngFor="let member of members">
<div (click)="memberClicked('email',member)">{{member.lastname}}</div>
<div (click)="memberClicked('mobile',member)">{{member.firstname}}</div>
</div>
The directive can then be used like this:
<memberlist [members]="members" (clicked)="memberlistClicked($event)"></memberlist>
Then in the component where the memberlist directive is used it simple to get the $event:
memberlistClicked(event) {
console.log(event);
}
I haven't tested it, but this is roughly how you do it.
You can read more about directive here:
Angular.io directives
I became aware that the question was about AngularJS, so here's a hint on how to solve it in AngularJS. I still love TS though.
Here an idea on how to create a directive, which can communicate with a controller:
app.directive('memberlist', () => {
return {
require: '^^someController',
restrict: 'E',
scope: {
members: '=members',
},
templateUrl: '/templates/memberlist.html',
link: function (scope, someController) {
scope.memberClicked = (type, member) => {
someController.currentMember = {
type: type,
member: member
};
}
}
};
});
The require statement is used so AngularJS knows to require 'someController'.
Directive in AngularJS
Another way, I sometimes use(d) in AngularJS, is to broadcast an event and in a controller create an event listener. Here's a good explanation of how to use broadcast Broadcast events in AngularJS