美文网首页
angular 组件通信

angular 组件通信

作者: 牛马风情 | 来源:发表于2017-04-18 15:27 被阅读0次

原文链接 https://segmentfault.com/a/1190000008959575#articleHeader0

输入属性 (父组件 -> 子组件)


counter.component.ts

import { Component, Input } from '@angular/core';

@Component({
    selector: 'exe-counter',
    template: `
      <p>当前值: {{ count }}</p>
      <button (click)="increment()"> + </button>
      <button (click)="decrement()"> - </button>
    `
})
export class CounterComponent {
    @Input() count: number = 0;

    increment() {
        this.count++;
    }

    decrement() {
        this.count--;
    }
}

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'exe-app',
  template: `
   <exe-counter [count]="initialCount"></exe-counter>
  `
})
export class AppComponent {
  initialCount: number = 5;
}

输出属性 (子组件 -> 父组件)


counter.component.ts

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

@Component({
    selector: 'exe-counter',
    template: `
      <p>当前值: {{ count }}</p>
      <button (click)="increment()"> + </button>
      <button (click)="decrement()"> - </button>
    `
})
export class CounterComponent {
    @Input() count: number = 0;

    @Output() change: EventEmitter<number> = new EventEmitter<number>();

    increment() {
        this.count++;
        this.change.emit(this.count);
    }

    decrement() {
        this.count--;
        this.change.emit(this.count);
    }
}

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'exe-app',
  template: `
   <p>{{changeMsg}}</p> 
   <exe-counter [count]="initialCount" 
    (change)="countChange($event)"></exe-counter>
  `
})
export class AppComponent {
  initialCount: number = 5;

  changeMsg: string;

  countChange(event: number) {
    this.changeMsg = `子组件change事件已触发,当前值是: ${event}`;
  }
}

模板变量


child.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'child-component',
  template: `I'm {{ name }}`
})

export class ChildComponent {
  public name: string;
}

parent.component.ts

import {Component, OnInit} from '@angular/core';
import {ChildComponent} from './child-component.ts';

@Component({
  selector: 'parent-component',
  template: `
    <child-component #child></child-component>
    <button (click)="child.name = childName">设置子组件名称</button>
  `
})

export class ParentComponent implements OnInit {
  
  private childName: string;
  
  constructor() { }

  ngOnInit() { 
    this.childName = 'child-component';
  }
}

@ViewChild 装饰器


child.component.ts

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

@Component({
    selector: 'exe-child',
    template: `
      <p>Child Component</p>  
    `
})
export class ChildComponent {
    name: string = '';
}

app.component.ts

import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'my-app',
  template: `
    <h4>Welcome to Angular World</h4>
    <exe-child></exe-child>
  `,
})
export class AppComponent {

  @ViewChild(ChildComponent)
  childCmp: ChildComponent;

  ngAfterViewInit() {
    this.childCmp.name = 'child-component';
  }
}

相关文章

  • angular4 子组件向父组件通信

    之前我写过 父组件向子组件通信 :《angular4 父组件向子组件通信传值》https://www.jiansh...

  • Angular 4 事件冒泡

    Angular 组件和 DOM 元素通过事件与外部进行通信, Angular 事件绑定语法对于组件和 DOM 元素...

  • Vue 组件 / 通信

    子组件=》父组件 vue的组件之间的通信类似angular的父子层级之间通信,父组件获取子组件数据步骤大概如下: ...

  • angular 组件通信

    angular组件通信是很长常见的功能,现在总结下,常见通信主要用一下三种 父组件 => 子组件 子组件 => 父...

  • angular 组件通信

    原文链接 https://segmentfault.com/a/1190000008959575#articleH...

  • angular组件通信

    官网:https://angular.cn/guide/component-interaction[https:/...

  • angular组件通信

    拆分组件是一项必须的编程技能,符合高内聚低耦合的设计理念。 父子组件通信 父组件 father.html: fat...

  • 初识Angular2的组件通信

    Angular2中免不了进行父子组件的通信,现在就稍微的了解一下父组件怎么给子组件传值,首先,通过Angular-...

  • 自己总结的Angular2组建通信

    组件之间的通信 这里简单的记录自己在angular2中,使用组件通信的一些方法。方便自己以后的使用。 一、组件之间...

  • Angular 组件通信 5种方法

    组件化是Angular的核心概念,所以组件通信的使用就比较多而且很重要。 官方传送门?:https://angul...

网友评论

      本文标题:angular 组件通信

      本文链接:https://www.haomeiwen.com/subject/owofzttx.html