我是看到别人的代码实现双向绑定,但是我不知道怎么在子组件上接收这个值,求帮助,代码如下:
<h1>test</h1>
<div>
<label for="username">heheda:</label>
<input type="text" [(ngModel)]="username">
</div>
<app-test [(ngModel)]="username" ></app-test>
test.component.html
<div>
<h2>test area</h2>
<div>
<label for="test"></label>
<input type="text" [(ngModel)]='name'>
</div>
</div>
test.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
@Input('name') name = 'testName';
constructor() { }
ngOnInit() {
}
}
实现
ControlValueAccessor
见 rating component