在角度2中我想通过服务在两个单独的组件之间进行通信,但它们不是子级和父级。 服务组件:
import { Injectable, Inject } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SignUpService {
private openDialog = new Subject<any>();
openDialogObservable$ = this.openDialog.asObservable();
constructor(){}
public open(data: any) {
if (data) {
this.openDialog.next(data);
}
}
}
SignUpComponent:
import { Component, OnInit, Input, ViewChild } from '@angular/core';
import {SignUpService} from "../services/signup-service.service";
import { Subscription } from 'rxjs';
@Component({
selector: 'signup',
templateUrl: './signup.html',
})
export class SignUpComponent implements OnInit {
private SignUpSubscription: Subscription;
display:boolean = false;
constructor(private signUpService: SignUpService) {
}
ngOnInit() {
this.SignUpSubscription =
this.signUpService.openDialogObservable$.subscribe((res) => {
if (res == 'show') {
this.display = true;
}
});
}
showDialog() {
this.display = true;
}
hideDialog() {
this.display = false;
}
}
SignUp Html:
<div style="background-color: #4cae4c; width: 1000px" class="ui-rtl"
dir="rtl">
<p-dialog [(visible)]="display" width="200px" modal="true"
[responsive]="true" tabindex="-1" role="dialog" aria-
labelledby="mySmallModalLabel" aria-hidden="true" >
<p-header class="text-center">
SignUp
</p-header>
<form (ngSubmit)="onSubmit($event)" #signUpForm="ngForm">
<div class="group signup form-group" style="width:300px">
<input #firstName="ngModel" class="form-control"
[(ngModel)]="createuser.firstName" name="firstname" type="text" pattern="[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF][\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]+" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Name</label>
</div>
</form>
</p-dialog>
关于这个HTML应该说我已经使用primeng库进行模态并且它使用了特殊标记。 在标题组件中,我有一个按钮(注册),注册组件和模板和服务在一个单独的文件夹中。我想当用户点击该按钮时会打开一个模态框。 标题组件:
import {Component, OnInit, EventEmitter, Output, Input, ViewChild} from '@angular/core';
import {SignUpService} from "../services/signup-service.service";
import {Subscription} from 'rxjs';
@Component({
selector: 'header-component',
templateUrl: './header.component.html',
})
export class HeaderComponent {
private subscription: Subscription;
constructor(private signUpService: SignUpService
) {
}
showDialog() {
this.signUpService.open('show');
}
最后这是标题html:
<a type="button" (click)="showDialog()" >Sign Up</a>
问题:当我点击标题中的注册按钮时,模态框不会打开。 没有错误,但是当我在源代码中放置断点时,它不会转到signup.component.ts。问题是什么?我该如何解决? 感谢您以简单的方式提供帮助。 还有这两个类似于我的代码的链接。 Parent and children communicate via a service和 Example of Component Communication in Angular 2/4
答案 0 :(得分:1)
尝试偶数发射器
首先添加您想要传递的数据:
public pdfdisplayAdded$: EventEmitter<PdfDisplay>;
constructor(private http: Http) {
this.pdfdisplayAdded$ = new EventEmitter();
}
setPdfData(retval: any) {
this.pdfdisplay = retval;
this.pdfdisplayAdded$.emit(this.pdfdisplay);
return this.pdfdisplay;
}
然后订阅您想要获取该数据的事件
this.pdfDisplayService.pdfdisplayAdded$.subscribe((data: any) => this.setPdfStatus(data));
答案 1 :(得分:0)
试试这个:
将openDialog
公开:
export class SignUpService {
public openDialog = new Subject<any>();
//...
}
并订阅openDialog
:
export class SignUpComponent implements OnInit {
//...
constructor(private signUpService: SignUpService) {
}
ngOnInit() {
this.SignUpSubscription =
this.signUpService.openDialog.subscribe((res) => {
if (res == 'show') {
this.display = true;
}
});
//...