六、angular6-父子页面传值

1.父页面向子页面传值

app.component.html页面添加

<app-my-first-componnet [num]="100" [pHeros]="heros"></app-my-first-componnet>

num表示传单个值的方式,pHeros表示传递对象方式, num和pHeros表示子页面接收的属性名

页面完整代码

<!-- Toolbar -->
<div class="card-container" role="banner">
  <app-my-first-componnet [num]="100" [pHeros]="heros"></app-my-first-componnet>
</div>

子页面

子页面以@Input接收

// 声明对外暴露的接口, 用于接受父组件的数据源输入, 必须是Number类型,获取父组件的值
@Input() num: Number;
@Input() pHeros;

2.父页面获取子页面的值

1.app.component.ts引入子组件

//引入子组件
import { MyFirstComponnetComponent } from './my-first-componnet/my-first-componnet.component';

2. 定义子属性

 @ViewChild(MyFirstComponnetComponent)
  child:MyFirstComponnetComponent

父子页面完整代码

 app.component.html

<!-- Toolbar -->
<div class="card-container" role="banner">
  <app-my-first-componnet [num]="100" [pHeros]="heros"></app-my-first-componnet>
  <button (click)="getChildHeros()"  class="btn btn-primary">获取子组件的数据</button>
</div>

app.component.ts

import {Component, ViewChild} from '@angular/core';
import { MyFirstServiceService } from './my-first-service.service';
//引入子组件
import { MyFirstComponnetComponent } from './my-first-componnet/my-first-componnet.component';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular6-fundamentals';
  message = null;
  heros : Array<{name: string, strength: number, agile:number, intelligence: number, color: string}> = [
    {name:'神灵武士', strength : 120, agile : 66, intelligence: 55, color: '#2cff54'},
    {name:'恐怖利刃', strength : 60, agile : 58, intelligence : 130, color: '#659bff'},
    {name:'末日使者', strength : 65, agile : 135, intelligence : 75, color: '#ff4722'}
  ]
  @ViewChild(MyFirstComponnetComponent)
  child:MyFirstComponnetComponent
  getChildHeros = function () {
    console.log(this.child.heros);
  }
  constructor(private myFirstServiceService: MyFirstServiceService) {
    this.message = myFirstServiceService.message;
  }
}

my-first-componnet.component.ts

 

import {Component, ViewChild} from '@angular/core';
import { MyFirstServiceService } from './my-first-service.service';
//引入子组件
import { MyFirstComponnetComponent } from './my-first-componnet/my-first-componnet.component';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular6-fundamentals';
  message = null;
  heros : Array<{name: string, strength: number, agile:number, intelligence: number, color: string}> = [
    {name:'神灵武士', strength : 120, agile : 66, intelligence: 55, color: '#2cff54'},
    {name:'恐怖利刃', strength : 60, agile : 58, intelligence : 130, color: '#659bff'},
    {name:'末日使者', strength : 65, agile : 135, intelligence : 75, color: '#ff4722'}
  ]
  @ViewChild(MyFirstComponnetComponent)
  child:MyFirstComponnetComponent
  getChildHeros = function () {
    console.log(this.child.heros);
  }
  constructor(private myFirstServiceService: MyFirstServiceService) {
    this.message = myFirstServiceService.message;
  }
}

 my-first-componnet.component.html

<!--
  index 索引  first是否第一个 last是否最后一个 odd是否奇数 even是否偶数 循环时可以去掉,用到的话就加上
-->
<div class="container" style="margin-top: 10px;">
  <div class="row" *ngFor="let hero of heros;index as index; first as first; last as last; odd as odd; even as even;">
    <span class="my-label">英雄:</span><span>{{hero.name}}</span>
    <span class="my-label">力量:</span><span>{{hero.strength}}</span>
    <span class="my-label">敏捷:</span><span>{{hero.agile}}</span>
    <span class="my-label">智力:</span><span>{{hero.intelligence}}</span>
    <div class="input-group mb-3">
      <input  type="text" [(ngModel)]="hero.speed">
      <div class="input-group-append">
        <button class="btn btn-primary" type="button" (click)="elementFn(hero.color)">英雄主题色</button>
      </div>
    </div>
    <div>
      <span>输入的移动速度是:{{hero.speed}}</span>
    </div>
  </div>
  <div *ngIf="showGameName">{{gameName}}</div>
  <!-- 获取dom元素-->
  <div class="row" #msgBox>英雄主题色</div>
  <!-- 子组件获取父组件的值开始-->
    <div class="row">
      <h4>子组件获取父组件的传值:</h4>
      <h4>{{num}}</h4>
    </div>
    <div class="row">
      <h4>子组件获取父组件的对象:</h4>
      <ul *ngFor="let pHero of pHeros">
        <li>{{pHero.name}}</li>
      </ul>
    </div>
  <! -- 子组件获取父组件的值结束-->
</div>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裂魂人1214

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值