码迷,mamicode.com
首页 > 其他好文 > 详细

Angular之输入输出属性

时间:2018-04-27 02:33:18      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:exp   export   ini   ons   input   rom   str   alt   core   

一 父组件

company.component.ts

import { Component, OnInit } from ‘@angular/core‘;

@Component({
  selector: ‘app-company‘,
  templateUrl: ‘./company.component.html‘,
  styleUrls: [‘./company.component.css‘]
})
export class CompanyComponent implements OnInit {

  employee = ‘Tom‘;
  skill: string;

  constructor() { }

  ngOnInit() {
  }

  releaseSkill(skill: string) {
    this.skill = skill;
  }

}

 

company.compnent.html

<p>
  company works! {{skill}}
</p>
<app-employee [name]="employee" (releaseSkill)="releaseSkill($event);"></app-employee>

 

二 子组件

 

employee.component.ts

import { Component, OnInit, Input, Output, EventEmitter } from ‘@angular/core‘;

@Component({
  selector: ‘app-employee‘,
  templateUrl: ‘./employee.component.html‘,
  styleUrls: [‘./employee.component.css‘]
})
export class EmployeeComponent implements OnInit {

  @Input()
  name: string;

  skill: string;

  @Output()
  releaseSkill: EventEmitter<string> = new EventEmitter();

  constructor() { }

  ngOnInit() {
  }

  perform() {
    this.skill = ‘摄影‘;
    this.releaseSkill.emit(this.skill);
  }


}

 

employee.component.html

<p>
  employee works! {{name}}
</p>
<button type="button" (click)="perform();">表演</button>

 

三 运行效果

 

技术分享图片

 

Angular之输入输出属性

标签:exp   export   ini   ons   input   rom   str   alt   core   

原文地址:https://www.cnblogs.com/sea-breeze/p/8955349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!