[英]How to pass value from one component to another component in Angular2?
I am trying to pass a value from one component to another in Angular2. 我试图在Angular2中将值从一个组件传递到另一个。 So for example, I am trying to implement a component and set a data and title in that component and pass that data and title to another component which is embedded in it?
因此,例如,我正在尝试实现一个组件,并在该组件中设置数据和标题,然后将该数据和标题传递给嵌入其中的另一个组件?
Here is the code to better explain. 这是更好解释的代码。
<cst-multiselect [title]="Countries" [data] = ['one', 'two', three']></cst-multiselect>
I am trying to pass the values ' title ' and ' data ' to another component: 我正在尝试将值“ title ”和“ data ”传递给另一个组件:
<div ngbDropdown class="d-inline-block" [autoClose]="false">
<button class="btn btn-outline-primary" id="dropdownMenu1" ngbDropdownToggle>Selected Countries
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu1">
<cst-checkbox></cst-checkbox>
</div>
</div>
I would like to display the data I passed from cst-multiselect on cst-checkbox(which is a dropdownbox). 我想在cst-checkbox(这是一个dropdownbox)上显示从cst-multiselect传递的数据。 I would like to set the value and also the data in this dropdownbox.
我想在此下拉框中设置值以及数据。 Any help would really be appreciated as I have been trying out multiple methods but to no avail.
真的很感激任何帮助,因为我一直在尝试多种方法,但无济于事。 Thank you!
谢谢!
@Component({
selector: 'cst-multiselect',
template: '<cst-checkbox [title]="title" [data]="data"'
...
})
class CstMultiSelect {
@Input() title:string;
@Input() data:string[];
}
@Component({
selector: 'cst-checkbox',
...
})
class CstCheckbox {
@Input() title:string;
@Input() data:string[];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.