如何在Angular2中将值从一个组件传递到另一个组件? [英] How to pass value from one component to another component in Angular2?

查看:93
本文介绍了如何在Angular2中将值从一个组件传递到另一个组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Angular2中将值从一个组件传递到另一个.因此,例如,我正在尝试实现一个组件,并在该组件中设置数据和标题,然后将该数据和标题传递给嵌入其中的另一个组件?

I am trying to pass a value from one component to another in 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?

以下是用于更好解释的代码.

<cst-multiselect [title]="Countries" [data] = ['one', 'two', three']></cst-multiselect>

我正在尝试将值" 标题 "和" 数据 "传递给另一个组件:

I am trying to pass the values 'title' and 'data' to another component:

<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>

我想在cst-checkbox(这是一个下拉框)上显示我从cst-multiselect传递的数据.我想在此下拉框中设置值以及数据.由于我一直在尝试多种方法,但无济于事,我们将不胜感激任何帮助.谢谢!

I would like to display the data I passed from cst-multiselect on cst-checkbox(which is a dropdownbox). 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[];
}

这篇关于如何在Angular2中将值从一个组件传递到另一个组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆