Angular 4 execute function from another component

前端 未结 2 2017
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 02:56

I\'m build a webapp using Angular4 and I\'m trying to create a button to call functions on other components.

I was reading other similar questions here, but they hav

相关标签:
2条回答
  • 2020-12-13 03:38

    I didn't get time to test it but similar solution worked for me. the code created for your need.

    Create a service like this

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    import { Subject } from 'rxjs/Subject';
    
    @Injectable()
    export class MessageService {
        private _listners = new Subject<any>();
    
        listen(): Observable<any> {
           return this._listners.asObservable();
        }
    
        filter(filterBy: string) {
           this._listners.next(filterBy);
        }
    
    }
    

    then implement in your header component like this

    // header.component.ts
    @Component({
        selector: 'header',
        templateUrl: `
            <section class="container">
                <button (click)="clickFilter()">Open filter</button>
            </section>
        `
     })
    
    export class HeaderComponent {
         @Output() onFilter: EventEmitter = new EventEmitter();
    
         constructor(private _messageService: MessageService){}
         clickFilter():void {
             // this.onFilter.emit('Register click');
             this._messageService.filter('Register click');
         }
     }
    

    then use in your store component like this

    @Component({
        selector: 'store',
        template: `<article class="card">
                     Test
                  </article>`
    })
    
    export class StoreComponent {
        constructor(private _messageService: MessageService){
            this._messageService.listen().subscribe((m:any) => {
                console.log(m);
                this.onFilterClick(m);
            })
        }
    
        onFilterClick(event) {
            console.log('Fire onFilterClick: ', event);
        }
     }
    

    The concept is that you can use observable in a service and subscribe in the component where you want it (store.component) and can send event from anywhere in the app like i did in the header.component

    I hope it will help you

    0 讨论(0)
  • 2020-12-13 03:47

    I had a similiar situation, but didn´t want to make a service for this. I managed to pass it like callback. It would be something like this:

    header.component.ts

    @Component({
    selector: 'header',
    templateUrl: `
        <section class="container">
            <button (click)="myFunction()">Open filter</button>
        </section>
    `})
    export class HeaderComponent {
       @Input() myFunction: Function;
    }
    

    Then just to call it passing the function and parameter

    // store.html
    <header [myFunction]="functionOnStore"></header>
    

    And just declaring functionOnStore normally on store component

    0 讨论(0)
提交回复
热议问题