13 November 2018

Rxjs event click giữa hai components không có mối liên hệ Angular 2+

Rxjs Event Click
Hai components không có mối liên hệ 
app.component.html
Angular 2+
<first-app></first-app>

<second-app></second-app>
first.component.ts
Angular 2+
import { Component } from '@angular/core';
import { Service } from '../event.service';

@Component({
  selector: 'first-app',
  templateUrl: './first.component.html',
  styleUrls: ['./first.component.css']
})
export class FirstComponent {

  flag: boolean = false;

  constructor(private service: Service) { }

  onSubmit(event: any) {
    this.service.newEvent(this.flag = !this.flag);
  }
}
second.component.ts
Angular 2+
import { Component } from '@angular/core';
import { Service } from '../event.service';

@Component({
  selector: 'second-app',
  templateUrl: './second.component.html',
  styleUrls: ['./second.component.css']
})
export class SecondComponent {

  content: string = '';

  constructor(private service: Service) {
    this.service.responseEvent.forEach(event => this.content = event);
  }

}
event.service.ts
Angular 2+
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class Service {
    private subject = new Subject < any > ();
    //private subject = new BehaviorSubject<boolean>(false);

    newEvent(event) {
        this.subject.next(event);
    }

    get responseEvent() {
        return this.subject.asObservable();
    }
}
app.module.ts
Angular 2+
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { FirstComponent } from './first/first.component';
import { SecondComponent } from './second/second.component';
import { Service } from './event.service';

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent , FirstComponent, SecondComponent],
  bootstrap:    [ AppComponent ],
  providers: [Service]
})
export class AppModule { }



Related Posts:

  • Validate focus vào item invalid sau khi Click/Event Angular 2+npm i @ismaestro/ngx-scroll-to-first-invalid --save Github | demo Usage 1. Import the NgxScrollToFirstInvalidModule: import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angular/core';… Read More
  • Cách sử dụng @input và @output trong Angular 2+ Bước 1: Tạo project Angular 2 với cmd bấm vào đây Bước 2: Tạo component ng generate component input-output Tạo component thành công Cấu trức thư mục được tạo Component đã được tự động khai báo trong app.modul… Read More
  • Uncaught Error: Template parse errors is not a known element compiler.js: 2547 Uncaught Error: Template parse errors: 'app-login-box' is not a known element: 1. If 'app-login-box' is an Angular component, then verify that it is part of this module. 2. If 'app-login-box' is … Read More
  • Guards Routes Angular 2+ 1. Guard Types Có bốn loại guards khác nhau chúng ta có thể sử dụng để bảo về routes của mình: CanActivate: Quyết định việc một route được kich hoạt. CanActivateChild: Quyết định việc children routes được kich hoạt … Read More
  • Deploying an Angular App to Github Pages angular-cli-ghpages Deploying an Angular App to Github Pages Installation & Setup npm i angular-cli-ghpages --save-dev Usage Usage: 1, ng build --prod --base-href "https://<user-name>.github.io/<repositor… Read More

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang