Angular4_组件之间传值

子组件html

< a * ngIf= "currentNodeStatus !== '0'" ( click)= "navigateDetail(deliveryId,currentNodeStatus,nextNodeStatus)" style= " cursor:pointer;" >
< img src= "{{imgUrl}}" width= "32px" >
</ a >
子组件 .ts

@ Output() onSelectIcon = new EventEmitter< any>();
@ Input() deliveryId : string;
@ Input() currentNodeStatus : string;
@ Input() nextNodeStatus : string;

父组件 html

< td class= "iconText" >
< appc-deliveryProgress ( onSelectIcon)= "onGridSelected('delivered',od.deliveryId,od.deliveredStatus)" deliveryId= "{{od.deliveryId}}" currentNodeStatus= "{{od.deliveredStatus}}"></ appc-deliveryProgress>
</ td >

父组件 .ts

onGridSelected(phase : string, deliveryId : string, phaseStatus : string) {
this. router. navigate([ '/deliveryProgressDashboard/progressDashboardDetail'], { queryParams: { 'orderId': deliveryId, 'phase': phase, 'phaseStatus': phaseStatus } });
}


官方文档:

组件之间的交互

Component Interaction

本烹饪宝典包含了常见的组件通讯场景,也就是让两个或多个组件之间共享信息的方法。

要深入了解组件通讯的各个基本概念,在组件通讯文档中可以找到详细的描述和例子。

参见 在线例子 / 下载范例 。

通过输入型绑定把数据从父组件传到子组件。

HeroChildComponent 有两个输入型属性,它们通常带@Input装饰器

component-interaction/src/app/hero-child.component.ts

 
 
  1. import { Component, Input } from '@angular/core';
  2.  
  3. import { Hero } from './hero';
  4.  
  5. @Component({
  6. selector: 'app-hero-child',
  7. template: `
  8. <h3>{{hero.name}} says:</h3>
  9. <p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
  10. `
  11. })
  12. export class HeroChildComponent {
  13. @Input() hero: Hero;
  14. @Input('master') masterName: string;
  15. }

第二个@Input为子组件的属性名masterName指定一个别名master(译者注:不推荐为起别名,请参见风格指南).

父组件HeroParentComponent把子组件的HeroChildComponent放到*ngFor循环器中,把自己的master字符串属性绑定到子组件的master别名上,并把每个循环的hero实例绑定到子组件的hero属性。

component-interaction/src/app/hero-parent.component.ts

 
 
  1. import { Component } from '@angular/core';
  2.  
  3. import { HEROES } from './hero';
  4.  
  5. @Component({
  6. selector: 'app-hero-parent',
  7. template: `
  8. <h2>{{master}} controls {{heroes.length}} heroes</h2>
  9. <app-hero-child *ngFor="let hero of heroes"
  10. [hero]="hero"
  11. [master]="master">
  12. </app-hero-child>
  13. `
  14. })
  15. export class HeroParentComponent {
  16. heroes = HEROES;
  17. master = 'Master';
  18. }

运行应用程序会显示三个英雄:

Parent-to-child
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值