简体   繁体   English

Eventemitter 不会将数据从子组件发送到父组件

[英]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> &nbsp;
        <button class="btn btn-danger" (click)="BluePrintAdded()" type="button">Add Server</button> &nbsp;
        <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.

相关问题 Angular 9:父 function 未从子调用 EventEmitter.emit - Angular 9: Parent function not called on EventEmitter.emit from child 如何从父组件到子组件发射数据[Angular 2] - How to emit data from parent to child component [Angular 2] 角度6-子组件(内部)发出EventEmitter并等待父组件返回响应 - Angular 6 - Child (Inner) Component emit an EventEmitter and wait to Parent Component return a respond Angular EventEmitter 不会在子组件的父组件中触发 - Angular EventEmitter is not firing in parent component from child component 从子组件向ngBootstrap中的父组件发送EventEmitter - send EventEmitter from child component to parent component in ngBootstrap Angular 4 - 如何在没有EventEmitter的情况下从子组件调用父方法? - Angular 4 - How to call parent method from child component without EventEmitter? 如何在父组件中使用带有可观察的子组件的 EventEmitter(角度 2) - How to use EventEmitter from child component with observable in parent (angular 2) 从子组件到父组件的Angular 5 EventEmitter发出未定义 - Angular 5 EventEmitter from child to parent component emits undefined 如何将数据从父组件发送到特定的动态创建的子组件? - How to emit data from Parent Component to a specific Dynamically created Child Component? 我可以开始和停止从Angular 2的孩子组件EventEmitter监听发射方法吗? - Can i start and stop listening to emit method from child's component EventEmitter in Angular 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM