【发布时间】:2019-03-04 16:33:28
【问题描述】:
使用 angular6。
我有一个父组件和 2 个子组件。我正在尝试将数据从 child1 组件传递到 child2 组件,如下所示:
下面是我创建 @Output 的 child1 组件
--Child1--
import { Component, OnInit, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'ct-child1',
templateUrl: './child1.component.html',
styleUrls: ['./child1.component.css']
})
export class Child1Component implements OnInit {
@Output() eventClicked: EventEmitter<any> = new EventEmitter();
//Button click event on child - need to pass some values here, below is an example
nextClicked() {
this.eventClicked.emit('from clause');
}
}
父组件下面监听child1
--Parent--
<form class="form-horizontal" role="form">
<div class="row">
<ct-child1 (eventClicked)="childEventClicked($event)"></ct-child1>
<ct-child2></ct-child2>
</div>
</form>
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'ct-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
public clickedEvent: string;
childEventClicked(event) {
this.clickedEvent = event;
}
}
最后是child2组件,它正在尝试监听值
--Child2--
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'ct-child2',
templateUrl: './child2.component.html',
styleUrls: ['./child2.component.css']
})
export class Child2Component implements OnInit {
constructor() { }
@Input() event: string;
//Button click event on Child2 - But here this.event returns undefined
ValueFromChild1() {
alert(this.event);
}
ngOnInit() {
}
}
我在上面面临的问题是在 Child2Component 中的 ValueFromChild1() 事件中,this.event 的值是未定义的。不知道这里缺少什么或者我没有做 正确的东西。我可以看到从 Child1 正确传递给父级的值,但无法在 child2 中获取该值。
谁能指出上面遗漏了什么?
不确定我是否应该创建一个单独的问题,但除此之外,您能否告诉我如何创建一个模型、填充它然后传递它,而不是像上面那样传递一个字符串。
谢谢
--更新代码--
--Child1--
import { Component, OnInit, Output, EventEmitter} from '@angular/core';
import { MyModel } from './data.model';
@Component({
selector: 'ct-child1',
templateUrl: './child1.component.html',
styleUrls: ['./child1.component.css']
})
export class Child1Component implements OnInit {
myModelData = {} as MyModel;
@Output() eventClicked: EventEmitter<MyModel> = new EventEmitter();
selectedDdlValue: string = '';
dropDownChange() {
if (this.selectedDdlValue == '') {
this.eventClicked = null;
}
}
//Button click event on child - need to pass some values here, below is an example
nextClicked() {
this.myModelData.ddlValue = this.selectedDdlValue;
this.eventClicked.emit(this.myModelData);
}
}
--Parent--
<form class="form-horizontal" role="form">
<div class="row">
<ct-child1 (eventClicked)="childEventClicked($event)"></ct-child1>
<ct-child2 [event]="clickedEvent" *ngIf="clickedEvent"></ct-child2>
</div>
</form>
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'ct-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
public clickedEvent: string;
childEventClicked(event) {
this.clickedEvent = event;
}
}
--Child2--
import { Component, OnInit, Input } from '@angular/core';
import { MyModel } from './data.model';
@Component({
selector: 'ct-child2',
templateUrl: './child2.component.html',
styleUrls: ['./child2.component.css']
})
export class Child2Component implements OnInit {
constructor() { }
@Input() event: MyModel;
ngOnInit() {
}
}
【问题讨论】:
-
您没有绑定 parent 和 child2 之间的任何属性....
-
有什么理由不使用服务?
-
@Jota.Toledo 我按照本教程创建了我的代码。这是不正确的吗?
-
@Talg123 抱歉,我是新手,服务是更好的选择吗?
标签: angular