[英]Eventemitter would not emit the data from child to parent component
app-component.ts app-component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
test="";
type:string;
title=[{Sname:'Test',Scontent:'Test',Stype:'test'}];
public OnServerAdded(Data:{name:string,content:string}) {
this.type="Main";
this.title.push({Sname:Data.name,Scontent:Data.content,Stype: this.type});
}
}
app-component.html应用组件.html
<app-cockpit (Servercreated)="OnServerAdded($event)"></app-cockpit>
<hr>
<p *ngFor="let a of title">
{{a.Sname}}
</p>
cockpit.component.ts cockpit.component.ts
import { Component, OnInit,EventEmitter ,Output} from '@angular/core';
@Component({
selector: 'app-cockpit',
templateUrl: './cockpit.component.html',
styleUrls: ['./cockpit.component.css']
})
export class CockpitComponent implements OnInit {
@Output() Servercreated=new EventEmitter<{Servername:string,ServerContent:string}>();
@Output() Bluecreated=new EventEmitter<{Servername:string,ServerContent:string}>();
newServerName= "";
newServerContent = "";
Add="";
constructor() { }
public ServerAdded()
{
this.Servercreated.emit({Servername:this.newServerName,ServerContent:this.newServerContent});
this.Add="add"
}
public BlueAdded()
{
this.Servercreated.emit({Servername:this.newServerName,ServerContent:this.newServerContent});
}
ngOnInit() {
}
}
cockpit.component.html驾驶舱.component.html
<div class="container">
<div class="row">
<div class="col-md-12">
<p>Add new Servers or BluePrint</p>
<label>Server Name</label>
<input type="text" class="form-control" [(ngModel)]="newServerName">
<label>Server Content</label>
<input type="text" class="form-control" [(ngModel)]="newServerContent">
<br>
<button class="btn btn-primary" (click)="ServerAdded()" type="button">Add Server</button>
<button class="btn btn-danger" (click)="BluePrintAdded()" type="button">Add Server</button>
<p></p>
</div>
</div>
</div>
I am trying to use event emitter to emit an event from child to parent component and display it using ngFor.我正在尝试使用事件发射器从子组件到父组件发出事件并使用 ngFor 显示它。 The emitter doesn't work.
发射器不工作。 I am trying to emit event from Cockpit component to App-component and display few data using ngFor.
我正在尝试从 Cockpit 组件向 App 组件发出事件并使用 ngFor 显示少量数据。
I think you are watching the Angular 7- The Complete Guide Created by Maximilian Schwarzmüller on Udemy.我认为您正在观看 Angular 7 - 由 Maximilian Schwarzmüller 在 Udemy 上创建的完整指南。 It's really good and detailed course.
这真的是很好和详细的课程。 Just compare your code with the one who the instructor has made.
只需将您的代码与教师制作的代码进行比较。
The code you pasted here doesn't compile even!!您粘贴在这里的代码甚至无法编译!! so we cant give ua detailed solution.
所以我们无法给出详细的解决方案。 just some general solution is listed below:
下面列出了一些通用的解决方案:
In app-component.ts some parts are missing.Also make sure you followed camel case or pascal case in all methods and properties names.在 app-component.ts 中,某些部分丢失了。还要确保在所有方法和属性名称中都遵循驼峰式大小写或帕斯卡式大小写。
The reason why you don't see anything from ngFor is that in cockpit you emit an object with these properties:您看不到 ngFor 任何内容的原因是,在 cockpit 中,您发出了一个具有以下属性的对象:
public ServerAdded() {
this.Servercreated.emit({
Servername: this.newServerName,
ServerContent: this.newServerContent
});
this.Add = "add"
}
Servername and ServerContent.服务器名称和服务器内容。 But in the receiving side you try to push it like this:
但是在接收方,您尝试像这样推动它:
this.title.push({Sname:Data.name,Scontent:Data.content,Stype: this.type});
Try changing this line to this:尝试将此行更改为:
this.title.push({ Sname: Data.Servername, Scontent: Data.ServerContent, Stype: this.type });
Check this stackblitz for working version.检查此stackblitz以获取工作版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.