1

In my angularjs project, I am facing an issue with click from the html. My code module is as follows I have a header module and an auth module

import { Component } from '@angular/core';

@Component({
selector: 'layout-header',
templateUrl: './header.component.html'
})
export class HeaderComponent {
constructor() {}

}

In header.component.html I have added a click event, my intention is to call a function from other component click code is as follow

<ul>
  <li class="nav-item"><a class="nav-link" (click)="clickLogout($event)" routerLinkActive="active"> Logout </a> </li>
</ul>

"clickLogout" function is added on other component any if does'nt calling up If I add the same "clickLogout" in header.component.ts, it works.

But for some reason I need it on another component, So is there any option to trigger click of other component from view : (click)="clickLogout($event)"

I am using angularjs4, Somebody please advice!

Directory structure is as follows

app 
--auth 
----auth-logout.component.ts 
--shared 
----layout 
-------header.component.ts 
-------header.component.html

I need the click call on auth-logout.component.ts

4
  • from which component you want to call it ? Is there any parent child relationship with those 2 controllers? or are they just siblings ? Commented Sep 20, 2017 at 10:36
  • No parent child relation, just a sibling. I need click from header component template to call another component function which has no relation with header component Commented Sep 20, 2017 at 10:37
  • I have added the directory structure too Commented Sep 20, 2017 at 10:44
  • please refer below answer Commented Sep 20, 2017 at 11:00

1 Answer 1

3

You Need a shared service to do so :

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class MessageService {
    private subject = new Subject<any>();

    logout() {
        this.subject.next({ text: 'logout'});
    }

    getMessage(): Observable<any> {
       return this.subject.asObservable();
    }

}

and in header component :

import { Component } from '@angular/core';
import { MessageService} from 'service/MessageService'; //import service here as per your directory
@Component({
    selector: 'layout-header',
    templateUrl: './header.component.html'
})
export class HeaderComponent {
   constructor(private messageService: MessageService) {}
    clickLogout(): void {
    // send message to subscribers via observable subject
    this.messageService.logout();
  }
}

And in any other component EDIT:

import { Component } from '@angular/core';
import { Subscription } from 'rxjs/Subscription'; //Edit
import { MessageService} from 'service/MessageService'; //import service here as per your directory
@Component({
        selector: 'another-component',
        templateUrl: './another.component.html'
    })
    export class AnotherComponent {
       constructor(private messageService: MessageService) {
    // subscribe to home component messages
            this.messageService.getMessage().subscribe(message => { 
               //do your logout stuff here
          });
        }

      ngOnDestroy() {
         // unsubscribe to ensure no memory leaks
        this.subscription.unsubscribe();
      }

    }

Reference taken from here.

Sign up to request clarification or add additional context in comments.

10 Comments

When I click logoutbutton clickLogout() in header component called up successfully => then logout function of MessageService called up successfully => but finally this.logoutService.getMessage().subscribe(message => { }); in AnotherComponent didnt called up
add console.log inside subscribe... does it printed the something ? actually once logout method get fired; subscribe method will automatically get called.
I am assuming another component will be in view correct ?
have u imported logout service in another component ? any error on console ?
click is from header component template, another component doest have any view and console.log inside subscribe... doesnt print
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.