【发布时间】:2017-08-23 18:16:13
【问题描述】:
您好,我正在尝试传递从其中一个选项中选择的值。 我使用 ngModel 来保存值,但我不知道如何将它传递给其他组件。由于它们已连接但未嵌套,因此我无法使用 Eventemitter,因为我认为使用 Eventemiiter,我应该使用子组件的选择器将组件嵌套在我不想做的父组件中。
这两个组件是分开的,我想将选定的值传递给另一个组件?我怎样才能做到这一点?
下面是我的代码。
组件 1 的模板
<div>
<select (change)="printValue()" formControlName="dogName"
class="form-control"
class="selectionbox"
[(ngModel)]="selected_dog" required>
<option *ngFor="let d of dogs" [ngValue]="d">
{{d.dogName}}
</option>
</select>
Component1的组件
selected_dog: string;
printValue () {
console.log (this.selected_dog)} // checked if the value is properly stored.
现在,我想将“selected_dog”值传递给 Component2。 组件2
value1: string; //how to pass selected_dog value to component2's value1.
我不确定要使用什么(eventEmitter,输出/输入?/ng-content?)
提前感谢您的帮助。
【问题讨论】:
-
component2是放在component1里面的吗?
-
不,它们是分开的,这就是为什么我无法使用 EventEmitter 实现我想要的,因为它们与未嵌套的路由相连
标签: angular components eventemitter