角度有自上而下的数据流方法。这意味着子对象通过输入从父对象获得,如果子对象必须告诉父对象一些事情,那么它必须显式地发出它想要告诉的内容
<parent-component>
<child-component (tellParent)="tellParent($event)" [giveToChild]="giveToChild">
</child-component>
</parent-component>
parent.component.ts:parent通过输入或方括号[]
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'parent-component',
templateUrl: './parent-component.html',
styleUrls: ['./parent-component.css']
})
export class ParentComponent implements OnInit {
giveToChild:string = 'Toffee';
constructor() { }
ngOnInit() {
}
tellParent(childResponse){
console.log(childResponse) // "Thanks dada and mama"
}
}
子组件.ts
import { Component, OnInit,Input } from '@angular/core';
@Component({
selector: 'Child-component',
templateUrl: './Child-component.html',
styleUrls: ['./Child-component.css']
})
export class ChildComponent implements OnInit {
@Input() giveToChild:string
@Output() tellParent = new EventEmitter<any>();
constructor() { }
ngOnInit() {
}
tellParentFunc(){
this.tellParent.emit("Thanks dada and mama")
}